相关疑难解决方法(0)

使用函数指针调用虚拟成员函数的基类定义

我想使用成员函数指针调用虚函数的基类实现.

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().
    }
};
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,func的派生类实现将从callFunc中调用.有没有办法可以保存指向Base :: func的成员函数指针,还是我必须以using某种方式使用?

在我的实际应用程序中,我使用boost :: bind在callFunc中创建一个boost :: function对象,我后来用它从程序的另一部分调用func.因此,如果boost :: bind或boost :: function有某种方法来解决这个问题也会有所帮助.

c++ virtual function-pointers boost-bind

19
推荐指数
1
解决办法
6129
查看次数

标签 统计

boost-bind ×1

c++ ×1

function-pointers ×1

virtual ×1