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

Mat*_*coe 27 c++ pointers class function member

我试图将类中的成员函数传递给一个带有成员函数类指针的函数.我遇到的问题是我不确定如何使用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)

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

Com*_*ger 33

要通过指针调用成员函数,您需要两件事:指向对象的指针和指向函数的指针.你需要两个MenuButton::SetButton()

template <class object>
void MenuButton::SetButton(int xPos, int yPos, LPCWSTR normalFilePath,
        LPCWSTR hoverFilePath, LPCWSTR pressedFilePath,
        int Width, int Height, object *ButtonObj, void (object::*ButtonFunc)())
{
  BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);

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

然后你可以使用两个指针来调用函数:

((ButtonObj)->*(ButtonFunc))();
Run Code Online (Sandbox Code Playgroud)

不要忘记将指针传递给您的对象MenuButton::SetButton():

testMenu::testMenu()
  :MenuScreen("testMenu")
{
  x.SetButton(100,100,TEXT("buttonNormal.png"), TEXT("buttonHover.png"),
        TEXT("buttonPressed.png"), 100, 40, this, test2);
  draw = false;
}
Run Code Online (Sandbox Code Playgroud)

  • `指向类的指针 - 应该是"指向对象的指针"? (2认同)

Mat*_*ank 15

我强烈推荐boost::bind和这样boost::function的事情.

请参阅传递并调用成员函数(boost :: bind/boost :: function?)


Yua*_* Li 6

我知道这是一个相当古老的话题.但是有一种优雅的方法可以用c ++ 11来处理这个问题

#include <functional>
Run Code Online (Sandbox Code Playgroud)

像这样声明你的函数指针

typedef std::function<int(int,int) > Max;
Run Code Online (Sandbox Code Playgroud)

声明你将这个东西传递给你的函数

void SetHandler(Max Handler);
Run Code Online (Sandbox Code Playgroud)

假设您将正常函数传递给它,您可以像平常一样使用它

SetHandler(&some function);
Run Code Online (Sandbox Code Playgroud)

假设你有一个成员函数

class test{
public:
  int GetMax(int a, int b);
...
}
Run Code Online (Sandbox Code Playgroud)

在您的代码中,您可以std::placeholders像这样使用它

test t;
Max Handler = std::bind(&test::GetMax,&t,std::placeholders::_1,std::placeholders::_2);
some object.SetHandler(Handler);
Run Code Online (Sandbox Code Playgroud)


GKe*_*lly 5

你是不是更好地使用标准OO.定义一个契约(虚拟类)并在你自己的类中实现它,然后只需将引用传递给你自己的类,让接收者调用契约函数.

使用您的示例(我已将'test2'方法重命名为'buttonAction'):

class ButtonContract
{
  public:
    virtual void buttonAction();
}


class testMenu : public MenuScreen, public virtual ButtonContract
{
  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);
      draw = false;
    }

    //Implementation of the ButtonContract method!
    void buttonAction()
    {
      draw = true;
    }
};
Run Code Online (Sandbox Code Playgroud)

在接收器方法中,您将引用存储到ButtonContract,然后当您想要执行按钮的操作时,只需调用存储的ButtonContract对象的'buttonAction'方法.