函数指针 成员函数指针


class CMyClass
{
public:
 CMyClass()
 {
  mm.insert(make_pair(1,&CMyClass::func1));

  func2();
 }

 typedef void (CMyClass::*func)();//mark
protected:
 void func1();
 void func2();
protected:
 map<int, func> mm;

};
void CMyClass::func1()
{
 cout<<"func1"<<endl;
}
void CMyClass::func2()
{
 // 成员函数的指针一定要用某个对象来调用

 // 不用指针调用的话,编译报错 类似error C2064: 项不会计算为接受 0 个参数的函数

 map<int, func>::iterator iter = mm.find(1);
 (this->*(*iter).second)();

 (this->*mm[1])();
}

发布了17 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/toothing/article/details/50188173