本帖最后由 daming 于 2014-12-30 02:15 编辑
- #include<iostream>
- using namespace std;
- class Point //类定义
- {
- public: //外部接口
- Point(float xx=0,float yy=0){X=xx;Y=yy;}
- float GetX(){return X;}
- float GetY(){return Y;}
- private:
- float X,Y;
- };
- void main()
- {
- Point A(4,5); //声明对象A
- Point *p1=&A; //声明对象指针并初始化
- float (Point:: *p_GetX)()=&Point::GetX; //声明成员函数指针并初始化 (类名::*指针名)(形参表)=&类名::成员函数名
-
- cout<<(A.*p_GetX)()<<endl; //通过成员函数指针访问成员函数 (对象名.*指针名)(参数表) /
- cout<<p1->GetX()<<endl; //通过对象指针访问成员函数 (对象指针名->*指针名)(参数表)
- cout<<A.GetX()<<endl; //通过对象访问成员函数
- }
复制代码
|