0

I have a struct A that I would like to use as a wrapper for various classes containing member functions with the same signature.

struct A
{
    int i;

    A()
    : i(0)
    {
    }

    int Do(std::function<int()> Function)
    {       
        i = Function();
        // Do stuff with i
        return i;
    }
};

An example would be:

class B : public A
{
public:

    int Func0() { return 4; }
    int Func1() { return 8; }

    void Interface()
    {
        std::cout << Do(Func0) << std::endl;
        std::cout << Do(Func1) << std::endl;
    }
};

But this is clearly wrong, how can I achieve this without using the C-style approach? (int (*f)())

AathakA
  • 117
  • 7
  • Pointer to member functions are very different from pointer to free functions or ordinary pointers. In fact, [pointers to member function are not actually pointers](https://stackoverflow.com/questions/72167059/are-pointers-to-non-static-member-function-formally-not-considered-pointers). – Jason Aug 08 '22 at 11:36
  • Your `Func0` and `Func1` do not require a `this` pointer, and could therefore be made `static`. If you did this, your code would work. – Drew Dormann Aug 08 '22 at 12:08

0 Answers0