注册 登录
编程论坛 C++教室

成员函数指针的初始化

chen3bing 发布于 2010-06-17 15:48, 1148 次点击
我照着一本书的例子写了个程序编译出错:
#include<iostream>
using namespace std;
class Point{
      public:
             Point(int xx=0,int yy=0){X=xx;Y=yy;}
             int GetX() {return X;}
             int GetY() {return Y;}
      private:
              int X,Y;
};
int main()
{Point A(4,5);
 Point *pp=&A;
 int (Point::*p_GetX)()=Point::GetX;   //声明成员函数指针并初始化
 
 cout<<A.GetX()<<"\t";
 cout<<(A.*p_GetX()<<"\t";
 cout<<pp->GetX()<<"\t";
 cout<<(pp->*p_GetX)()<<endl;
 getch();
}
注释的那一行有错:
14 E:\小软件\C++\pp.cpp invalid use of non-static member function `int Point::GetX()'
请高手指教,谢谢!
2 回复
#2
chen3bing2010-06-17 15:57
高手支招啊!
#3
chen3bing2010-06-17 16:10
知道了,呵呵
int (Point::*p_GetX)()=&Point::GetX;
1