forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_pointer.cpp
More file actions
35 lines (26 loc) · 762 Bytes
/
method_pointer.cpp
File metadata and controls
35 lines (26 loc) · 762 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
/*
# Method pointer
# .* operator
# ->* operator
Yes, `.*` and `->*` are actual C++ only operators
dedicated to calling member method pointers.
http://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer
*/
#include "common.hpp"
int main() {
class C {
public:
int i;
C(int i) : i(i) {}
int m(int j) { return this->i + j; }
};
int (C::*p)(int) = &C::m;
C c(1);
C *cp = &c;
// Operator .*
assert((c.*p)(2) == 3);
// Operator ->*
assert((cp->*p)(2) == 3);
// Bound this: TODO Impossible? Things accept the `this` as the next argument however:
// http://stackoverflow.com/questions/10673585/start-thread-with-member-function
}