首页 > 其他 > 详细

类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

时间:2017-03-02 12:23:40      阅读:145      评论:0      收藏:0      [点我收藏+]

you can get the pointer of the method, but it has to be called with an object

typedef void (T::*MethodPtr) ();
MethodPtr method = &T::MethodA;
T *obj = new T();
obj->*method();

If you need to have non-object pointer and you want to use object then you have to store instance of object somewhere, but you are restricted to use only one object (singleton).

class T {
  static T *instance;
public:
  T::T() {
    instance = this;
  }
  static void func() {
    instance->doStuff();
  }
  void doStuff() {}
};

If library supports user data for function pointers, then you may have multiple instances

class T {
public:
  static void func(void *instance) {
    ((T*)instance)->doStuff();
  }
  void doStuff() {}
};

类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

原文:http://www.cnblogs.com/diegodu/p/6489788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!