jeudi 3 mars 2016

How to store a virtual member function in 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 { }
}

I want to assign overrided 'doSomething' function into 'foo::f'. but I have no idea how can I assign overrided 'doSomething' function. or I just write some code for assigning 'doSomething' function for each class?

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) 
    {  
        this->f = std::bind(&bar::doSomething, this);
    }

private:
    virtual void doSomething(void) override { }
}

That code is my answer of my question. but I think I can assign virtual function into std::function automatically.

Aucun commentaire:

Enregistrer un commentaire