Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing member function to another object's member function C++

I am having issues trying to pass a function as an argument in another object's function. I am well aware there are many similar topics but I either can't get their solution to work or can't understand them.

class foo
{
public:
    void func1(void (*Drawing)(void));

    template<class T>
    void func2(void (T::*Drawing)(void));
};

class bar
{
private:
    foo myFoo;
    void Drawing();
    void func3() {
        // Attempt 1
        myFoo.func1(Drawing);

        // Attempt 2
        myFoo.func2<bar>(&bar::Drawing);
    }
};

So in my first attempt, I get the error where you can't convert void (bar::*)(void) to void (*)(void) of which I then found out there are normal function pointers and member function pointers.

Attempt 2 was my feeble attempt to overcome this but I get unresolved externals now...

So how can I successfully pass my Drawing() member function into another function from another object?

like image 915
Enriel Avatar asked Sep 16 '26 08:09

Enriel


1 Answers

The issue is that you cannot consider bar::Drawing as a void (*)(void) function since it's a non static method, which therefore required an object (the this context which will be used)

A solution, assuming c++11 is ok for you, would be to use std::bind and to sligtly modify your foo definition:

class foo
{
    public:
    void func1(std::function<void(void)> Drawing)
    {
        Drawing(); // or do whatever you want with it
    }
};

Then you will be able to do

void bar::func3() {
    myFoo.func1(std::bind(&bar::Drawing, this));
}

making valid a lot of potential uses

int main()
{
    bar myBar;
    myBar.func3();

    foo myFoo;
    myFoo.func1([](){ printf("test\n"); });
    return 0;
}
like image 162
Amxx Avatar answered Sep 17 '26 22:09

Amxx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!