C++学习笔记 指向类的数据成员的指针

 1 class A
 2 {
 3 private:
 4     int a;
 5 public:
 6     int c;
 7     A(int i)
 8     {
 9         a = i;
10     }
11     int func(int b)
12     {
13         return a * c + b;
14     }
15 };
16 int main() {
17 
18     A x(18);
19     int A::* pc = &A::c;//定义指针指向A类的C成员
20     x.*pc = 5;
21     cout << x.c << " " << x.*pc << ".";
22     system("pause");
23     return 0;
24 }

二,指向函数的指针

1 int(A:: * pfunc)(int) = &A::func;
//int (A::*pfunc)(int)=&A::func;指针指向函数返回值是int,参数是int
2 cout << (x.*pfunc)(10) << endl;//
 

三,指向类的指针

1 A* p = &x;//对象名.*指向类的成员函数指针名(参数表)
2 cout << (p->*pfunc)(10) << endl;//(对象指针名->*指向类成员函数指针名)(参数表)
原文地址:https://www.cnblogs.com/qq964107326/p/15008611.html