First commit
[cpp_sandbox.git] / Sandbox / PointerToMember.cpp
1 #include "PointerToMember.h"
2
3 #include <iostream>
4 using namespace std;
5
6 namespace PointerToMember
7 {
8 struct Test {
9 int num;
10
11 Test(int n)
12 {
13 this->num = n;
14 }
15
16 void func()
17 {
18 cout << "func called, this->num = " << this->num << endl;
19 }
20 };
21
22 int Test::* ptr_num = &Test::num; // Pointer to a field of 'Test'.
23 void (Test::* ptr_func)() = &Test::func; // Pointer to a method of 'Test'.
24 }
25
26 void PointerToMember::tests()
27 {
28 Test t{ 42 };
29 Test* pt = new Test{ 420 };
30
31 (t.*ptr_func)();
32 (pt->*ptr_func)();
33 }