非静态成员的指针
一、定义
类型说明符 类名::指针名
//指向公有数据成员的指针
类型说明符 (类名::*指针名)(参数表)
//指向公有成员函数的指针
二、赋值 //确定指向类的哪个成员
指针名
= &类名::数据成员名
指针名
= 类名::函数成员名
三、引用
对象名.类数据名成员指针名
(对象名.类函数成员)(参数表)
或
对象指针名->*类数据成员指针名
(对象指针名->*类函数成员指针名)(参数表)
例:
#include <iostream>
using namespace std;
class point
{
public:
int x1, x2;
public:
point(int x, int y)
{
x1=x;
x2=y;
}
point()
{
x1=0;
x2=0;
}
int getX()
{
return x1;
}
int getY()
{
return x2;
}
};
int main()
{
point data(3, 4), *p, *q;
int point::*r;
int (point::*qq)();
r = &point::x1;
qq = &point::getX;
p = &data;
cout << p->getY() << endl;
cout << data.*r << endl;
cout << (data.*qq)() << endl;
return 0;
}