Call C++ member function pointer without knowing which class

Viewed 1382

I am trying to call a member function, possibly given the object pointer, without knowing what class the member function is from. Is this possible?

Basically I want something like the following to work.

class Foo{
public:
    Foo(void* object): obj(object) {}

    void callFunc(void (*func)()){
        obj->*func();
    }

private:
    void* obj;
};

class Bar{
public:
    Bar(): foo(this) {}

    void callSomeFunc(){
        callFunc(someFunc);
    }

    void someFunc(){
        cout << "hi\n";
    }

private:
    Foo foo;
};

int main(){
    Bar bar;
    bar.callSomeFunc();
    return 0;
}
3 Answers
Related