相关疑难解决方法(0)

你如何传递成员函数指针?

我试图将类中的成员函数传递给一个带有成员函数类指针的函数.我遇到的问题是我不确定如何使用this指针在类中正确执行此操作.有没有人有建议?

这是传递成员函数的类的副本:

class testMenu : public MenuScreen{
public:

bool draw;

MenuButton<testMenu> x;

testMenu():MenuScreen("testMenu"){
    x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2);

    draw = false;
}

void test2(){
    draw = true;
}
};
Run Code Online (Sandbox Code Playgroud)

函数x.SetButton(...)包含在另一个类中,其中"object"是模板.

void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {

    BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);

    this->ButtonFunc = &ButtonFunc;
}
Run Code Online (Sandbox Code Playgroud)

如果有人对如何正确发送此功能有任何建议,以便我以后可以使用它.

c++ pointers class function member

27
推荐指数
4
解决办法
4万
查看次数

标签 统计

c++ ×1

class ×1

function ×1

member ×1

pointers ×1