BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #16840同步于 2008/11/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

【帮助】成员函数指针 问题

unicornhx
2008/11/28镜像同步2 回复
遍了个小程序 试验成员函数指针 结果提示报错 是不是vc语法有变化? #include <iostream> using namespace std; class Mammal { public: Mammal(){HowMany ++;cout << "the number of Mammal is "<< HowMany<<endl;} ~Mammal(){HowMany--;cout << "the number of Mammal is "<< HowMany<<endl;} void Getage() const {cout<< "it's age is " << itsAge<<endl; } static void GetNum() {cout << "Now the number of Mammal is : "<< HowMany<<endl;} private: int itsAge; static int HowMany; }; int Mammal::HowMany=0; int main () { const int MaxNum=3; void (Mammal::*pget)() const=0; pget=Mammal::Getage; // 这里提示说要加个& Mammal a; //提示说 error C2039: 'pget' : is not a member of 'Mammal' a.pget; a.GetNum(); return 0; }
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
wks机器人#1 · 2008/11/29
同问。 p.s. 这是不是个闭包?pget肯定储存了这个对象的存储地址,和函数的入口地址。
bird机器人#2 · 2008/11/29
【 在 unicornhx 的大作中提到: 】 : 遍了个小程序 试验成员函数指针 结果提示报错 是不是vc语法有变化? : #include <iostream> : using namespace std; : ................... #include <iostream> using namespace std; class Mammal { public: Mammal() {HowMany ++; cout << "the number of Mammal is " << HowMany << endl; } ~Mammal() {HowMany --; cout << "the number of Mammal is " << HowMany << endl;} void Getage() const {cout << "it's age is " << itsage << endl; } static void GetNum() {cout << "Now the number of Mammal is : " << HowMany << endl;} private: int itsage; static int HowMany; }; int Mammal::HowMany = 0; int main() { const int MaxNum = 3; void (Mammal::*pget)() const = 0; pget = &Mammal::Getage; Mammal a; (a.*pget)(); a.GetNum(); return 0; }