-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreflection.cpp
More file actions
45 lines (29 loc) · 741 Bytes
/
reflection.cpp
File metadata and controls
45 lines (29 loc) · 741 Bytes
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
#include <cstdlib>
#include <iostream>
using namespace std;
class Bar {
public:
Bar(std::string thoughts) : _thoughts(thoughts) {
}
void foo(void) {
cout << "Foolicious: " << _thoughts << endl;
}
private:
std::string _thoughts;
};
int main() {
// Pointer to method:
void (Bar::* method) (void) = &Bar::foo;
// Works for stack:
Bar bar("Quite indeed.");
(bar.*method)();
// Works for heap:
Bar* barPointer = new Bar("You are wrong.");
(barPointer->*method)();
// Now we've got that covered: How about using the auto keyword?
auto method2 = &Bar::foo;
(bar.*method2)();
// No memory leakage in this example.
delete barPointer;
return 0;
}