1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5
6 class Test{
7 private:
8 string name;
9 public:
10 string GetName(){
11 return name;
12 }
13 void SetName(string name){
14 this->name=name;
15 }
16
17 };
18
19
20
21
22 int main(){
23 cout<<"----------------"<<endl;
24
25 Test* t = new Test();
26
27 //方案1 使用 auto, 不用 GetPtr 或 SetPtr 函数定义
28 //auto funcGet = &Test::GetName;
29 //auto funcSet = &Test::SetName;
30
31 //(t->*funcSet)("张三");
32 //string name = (t->*funcGet)();
33 //cout<<name<<endl;
34
35
36
37 //方案2 先定义两个 函数定义, 然后再调用这个函数定义
38 typedef string (Test::*GetPtr)();
39 typedef void (Test::*SetPtr)(string);
40
41 GetPtr funcGet = &Test::GetName;
42 SetPtr funcSet = &Test::SetName;
43
44 (t->*funcSet)("张三");
45 string name = (t->*funcGet)();
46 cout<<name<<endl;
47
48
49
50
51 cout<<"----------------"<<endl;
52 cin.get();
53 }