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

人走茶凉 提交于 2020-01-04 23:45:55

问题


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?


回答1:


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;
}



回答2:


I'm guessing you've left out important details as to what you're trying to accomplish. However, the following should give you some idea of what you need to do.

Code

#include <iostream>

class foo
{
public:
    void func1(void (*Drawing)(void))
    {
        std::cout << "func1\n";
    }

    template<class T>
    void func2(T* instance, void (T::*fn)(void))
    {
        std::cout << "func2: ";
        (instance->*fn)();
    }
};

class bar
{
public:
    bar()
    {
        func3();
    }

private:
    foo myFoo;

    void Drawing()
    {
        std::cout << "bar::Drawing()\n";
    }

    void func3()
    {
        // Attempt 1
        //myFoo.func1(Drawing); // This won't work

        // Attempt 2
        myFoo.func2(this, &bar::Drawing); // Must pass an object instance if you plan to use the member function
    }
};

int main(int argc, char *argv[])
{
    bar b;

    return 0;
}

Sample Output

func2: bar::Drawing()


来源:https://stackoverflow.com/questions/29240317/passing-member-function-to-another-objects-member-function-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!