C++ 特殊类成员 8-- 9.1类的函数指针

#include <iostream>
using namespace std;
/*---------------------------------
     17-09 9.1类的函数指针
  1) 指向类的成员函数的指针,即类的函数指针
---------------------------------*/
class A
{
public:
void Set(int x,int y)
{
a=x;
b=y;
}
void show(){cout<<"a:"<<a<<"\tb:"<<b<<endl;}
private:
int a;
int b;
};
void (A::*p)(int,int);
int main()
{
A a;
p=A::Set;
//p=&A::Set; //上面那种和下面这种都能正常运行
(a.*p)(1,3); //注意,p前面要加*
a.show();
return 0;

}

运行结果:

a:1     b:3
Press any key to continue

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80497612