小编sop*_*hia的帖子

如何在std :: function中存储虚拟成员函数?

class foo
{
public:
    foo(void)
    {
        this->f = std::bind(&foo::doSomething, this);
    }

private:
    virtual void doSomething(void) { }

private:
    std::function<void(void)> f;
}

class bar : public foo
{
public:
    bar(void) { /* I have no idea what I have to */ }

private:
    virtual void doSomething(void) override { }
}
Run Code Online (Sandbox Code Playgroud)

我想将覆盖的'doSomething'函数分配给'foo :: f'.但我不知道如何分配重写'doSomething'功能.或者我只是编写一些代码来为每个类分配'doSomething'函数?

class foo
{
public:
    foo(void)
    {
        this->f = std::bind(&foo::doSomething, this);
    }

private:
    virtual void doSomething(void) { }

private:
    std::function<void(void)> f;
}

class bar : public foo
{
public: …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 std-function

2
推荐指数
1
解决办法
1351
查看次数

标签 统计

c++ ×1

c++11 ×1

std-function ×1