Search code examples
c++vectorenumsstdstd-function

std::vector that holds class methods


I have done this to enumerate my class members

enum MemberType {A, B, C, D};

class Hello
{
public:
    std::vector<std::function<void(Hello*, void)>>     m_members;

    void func()
    {
    };

    Hello()
    {
        m_members[A] = func();
    }
};

What is wrong here?


Solution

  • First, you had an extra void:

    std::vector<std::function<void(Hello*)>>     m_members;
    

    Then, you need to assign something more like this:

    m_members[A] = &Hello::func;
    

    Or you can store bound functions, in which case std::function<void()> and std::bind(&Hello::func, this).