本帖最后由 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; //通過對象訪問成員函數
- }
復制代碼
|