-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.cpp
More file actions
70 lines (43 loc) · 1.03 KB
/
pointer.cpp
File metadata and controls
70 lines (43 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
void pointer_to_member() {
struct S { unsigned m; };
unsigned S::*ptr = &S::m;
S s = {10};
std::cout << s.*ptr << std::endl;
S* sp = &s;
sp->m = 1;
std::cout << sp->*ptr << std::endl;
}
void base_derived_pointers() {
struct Base { int m; };
struct Derived: Base { };
int Base::*ptr = &Base::m;
int Derived::*ptr2 = &Derived::m;
Derived d;
d.m = 10;
std::cout << d.*ptr << std::endl;
std::cout << d.*ptr2 << std::endl;
}
void pointer_to_member_function() {
struct S {
void foo() {
std::cout << "S::foo" << std::endl;
}
static void bar() {
std::cout << "S::bar" << std::endl;
}
int m = 0;
};
int S::*p = &S::m;
void (*SPtr)() = &S::bar;
void (S::*Sptr2)() = &S::foo;
S s;
std::cout << s.*p << std::endl;
(*SPtr)();
(s.*Sptr2)();
}
int main() {
pointer_to_member();
base_derived_pointers();
pointer_to_member_function();
}