Search code examples
c++function-pointersclass-membersmember-pointers

Can a single function pointer point to multiple classes member function


Here are the requirements posed by my application. I have a class A, that accepts a function pointer say cFunc, Basically in my implementation of A, I have it call cFunc multiple times.

The cFunc pointer itself should point to different functions depending upon the application. Thus for each application I create a class with the same function definition as cFunc, however I cannot assign the class's member function to this pointer

class A {
    typedef double (*Def_CFunc)(std::vector<double>);
    A(Def_CFunc _cFunc) { // Some implementation}
    // Other Functions
};

class B { double someFunc(std::vector<double> b); };
class C { double someOtherFunc(std::vector<double> a); };

int main () {
    B firstObj;
    C secondObj;

    // Depending upon the situation, I want to select class B or C
    double (*funcPointer)(std::vector<double>) = firstObj.someFunc; // Error in this line of code

    A finalObj(funcPointer);
}

So how do I make it such that any class with a member function of the given format can be used to initialize the class A?


Solution

  • I'm not sure what exactly your requirements are, but it looks like you want an interface (or abstract base class in C++ lingo).

    If both B and C inherit from a common base class, you can pass a pointer to this base class and invoke functions on it:

    class I { virtual double func(std::vector<double> a) = 0; }
    class B : public I { double func(std::vector<double> a); };
    class C : public I { double func(std::vector<double> a); };
    

    You can pass an I* pointer to A and just use i->func.