标题: C++语言指向类的非静态成员函数的指针 [打印本页]

作者: daming    时间: 2014-12-30 02:00
标题: C++语言指向类的非静态成员函数的指针
本帖最后由 daming 于 2014-12-30 02:15 编辑


  1. #include<iostream>
  2. using namespace std;
  3. class Point                     //类定义
  4. {
  5. public:                        //外部接口
  6. Point(float xx=0,float yy=0){X=xx;Y=yy;}
  7. float GetX(){return X;}
  8. float GetY(){return Y;}

  9. private:
  10. float X,Y;
  11. };
  12. void main()
  13. {
  14. Point A(4,5);              //声明对象A
  15. Point *p1=&A;              //声明对象指针并初始化
  16. float (Point:: *p_GetX)()=&Point::GetX; //声明成员函数指针并初始化    (类名::*指针名)(形参表)=&类名::成员函数名

  17. cout<<(A.*p_GetX)()<<endl;        //通过成员函数指针访问成员函数      (对象名.*指针名)(参数表)  /
  18. cout<<p1->GetX()<<endl;           //通过对象指针访问成员函数            (对象指针名->*指针名)(参数表)
  19. cout<<A.GetX()<<endl;              //通过对象访问成员函数
  20. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1