27

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

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

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

4

6 回答 6

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

然后您可以使用两个指针调用该函数:

((ButtonObj)->*(ButtonFunc))();

不要忘记将指向对象的指针传递给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;
}
于 2008-09-24T23:11:53.577 回答
15

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

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

于 2008-09-24T22:46:47.397 回答
6

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

#include <functional>

像这样声明你的函数指针

typedef std::function<int(int,int) > Max;

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

void SetHandler(Max Handler);

假设你向它传递了一个普通函数,你可以像平常一样使用它

SetHandler(&some function);

假设你有一个成员函数

class test{
public:
  int GetMax(int a, int b);
...
}

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

test t;
Max Handler = std::bind(&test::GetMax,&t,std::placeholders::_1,std::placeholders::_2);
some object.SetHandler(Handler);
于 2015-04-15T20:03:51.527 回答
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;
    }
};

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

于 2008-09-25T08:11:18.803 回答
2

在极少数情况下,您碰巧使用 Borland C++Builder 进行开发并且不介意编写特定于该开发环境的代码(即不能与其他 C++ 编译器一起使用的代码),您可以使用 __closure 关键字. 我找到了一篇关于 C++Builder 闭包的小文章。它们主要用于 Borland VCL。

于 2008-09-24T23:10:23.237 回答
2

其他人已经告诉你如何正确地做到这一点。但令我惊讶的是,没有人告诉你这段代码实际上很危险:

this->ButtonFunc = &ButtonFunc;

由于 ButtonFunc 是一个参数,所以当函数返回时它会超出范围。你正在获取它的地址。您将获得一个类型的值void (object::**ButtonFunc)()指向成员函数指针的指针)并将其分配给 this->ButtonFunc。在您尝试使用 this->ButtonFunc 时,您会尝试访问(现在不再存在)本地参数的存储,并且您的程序可能会崩溃。

我同意 Commodore 的解决方案。但你必须将他的线路改为

((ButtonObj)->*(ButtonFunc))();

因为 ButtonObj 是一个指向对象的指针。

于 2008-11-23T16:58:18.937 回答