且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++指向成员函数的指针继承

更新时间:2021-11-13 09:54:30

,我建议你使用boost :: function为手头的任务。

If you can use boost libraries, I would suggest you use boost::function for the task at hand.

class A {
public:
   void doSomething( boost::function< void ( int ) > callback )
   {
      callback( 5 );
   }
};

然后任何继承(或外部类)都可以使用boost :: bind做调用:

Then any inheriting (or external class) can use boost::bind do make a call:

class B {
public:
   void my_method( int a );
};
void test()
{
   B b;
   A a;
   a.doSomething( boost::bind( &B::my_method, &b, _1 ) );
};

我没有检查确切的语法,我从顶部输入,至少接近正确的代码。

I have not checked the exact syntax and I have typed it from the top of my head, but that is at least close to the proper code.