我希望使用成员函数指针调用虚函数的基类实现。
class Base {
public:
virtual void func() { cout << "base" << endl; }
};
class Derived: public Base {
public:
void func() { cout << "derived" << endl; }
void callFunc()
{
void (Base::*fp)() = &Base::func;
(this->*fp)(); // Derived::func will be called.
// In my application I store the pointer for later use,
// so I can't simply do Base::func().
}
};
在上述代码中,从callFunc调用派生类实现的func。我是否可以保存指向Base :: func的成员函数指针,还是必须以某种方式使用
using
?在我的实际应用中,我使用boost :: bind在callFunc中创建boost :: function对象,稍后我将使用它来从程序的另一部分调用func。因此,如果boost :: bind或boost :: function有某种方法可以解决这个问题,那也会有所帮助。