简体   繁体   English

我可以将一个类中的函数传递给另一个类的构造函数,并将其存储在那里以便以后调用吗? C ++

[英]Can i pass a function from a class to a constructor of another class and store it there to call later? C++

So basically I'm making buttons in a game, and the buttons are a called Button. 所以基本上我是在游戏中制作按钮的,这些按钮称为“按钮”。 The class i want the function from to store is called SoccerLevelsClass. 我希望函数从中存储的类称为SoccerLevelsClass。 I've tried looking into function pointers, but I'm not sure what's going on though i think it's the correct thing to do. 我尝试研究函数指针,但是我不确定这是怎么回事,尽管我认为这是正确的做法。

I want to save the function of SoccerLevelsClass as a member of Button. 我想将SoccerLevelsClass的功能另存为Button的成员。 Would i do something like this? 我会做这样的事情吗?

//MenuButton.h

#ifndef MenuButton
#define MenuButton
....

class Button
{
public:
       Button(void(*SoccerLevelsClass::func)());
       void (*SoccerLevelsClass::function)();
       ....

}
#endif


//MenuButton.cpp
#include <MenuButton.h>
Button::Button(void(*SoccerLevelsClass::func)())
{
   function=func;     //something like this
}

I know the code is probably way off, but I'd like to know if anybody has any suggestions. 我知道代码可能还很遥远,但是我想知道是否有人有任何建议。 All i really want to know is if it's possible. 我真正想知道的是是否有可能。

Yes, this can be done - either with function pointers like in your example, or with lambdas if you can use C++11. 是的,可以做到这一点-可以使用示例中的函数指针,也可以使用lambdas(如果可以使用C ++ 11)。

However, since you want to call a bound function of another class, you would need to pass/store pointer to an instance of that class as well to do that, unless the function is static. 但是,由于您要调用另一个类的绑定函数,因此您也需要传递/存储指向该类实例的指针,除非该函数是静态的。

In C++11, this is trivial: 在C ++ 11中,这很简单:

std::function<void(void)> _f;

void apply() {
    _f();
}

Bar(void (Foo::* f)()) { 
    _f = std::bind(f, Foo());
}

In C++03, this is a little tricky. 在C ++ 03中,这有些棘手。 Note in both versions I construct a temporary to call the member function, but I'm not sure whether it is necessary to store an instance of the class. 请注意,在这两个版本中,我都构造了一个临时函数来调用成员函数,但是我不确定是否需要存储该类的实例。

#include <iostream>
#include <functional>

struct Foo
{
    Foo() { }
    void stuff() { 
        std::cout << "hi\n";
    }
};

struct Bar
{
    void (Foo::* _f)();

    void apply() {
        (Foo().*_f)();
    }

    Bar(void (Foo::* f)()) { 
        _f = f;
    }
};

int main()
{
    Bar bar(&Foo::stuff);
    bar.apply();
}

For what you are trying to do I would use the observer pattern: 对于您尝试做的事情,我将使用观察者模式:

class IFootballObserver
{
public:
    virtual void OnBallKicked() = 0;
    virtual ~IFootballObserver() {}
};

class Fooball
{
public:
     Fooball(IFootballObserver& obs)
      : mObs(obs)
    {
      // Call the observer interface at any time like so:
      mObs.OnBallKicked();
    }
private:
    IFootballObserver& mObs;
};

class Button : public IFootballObserver
{
public:
  // Football could be passed in/owned by something else
  Button() : mFootball(*this) { }

  void DoSomething()
  {
     // Called when foot ball is kicked
  }
private:
  virtual void OnBallKicked()
  {
      DoSomething();
  }

Fooball mFootball;
};

I find this easier than using function pointers/std::function. 我发现这比使用函数指针/ std :: function更容易。 Plus you could have a vector of observers and notify many objects of events. 另外,您可以有一个观察者向量,并通知许多事件对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C创建C ++类,我可以将C ++对象传递给C中的C ++构造函数吗? - Creating C++ class from C, can I pass C++ objects to C++ constructor in C? 构造函数可以在c ++中调用另一个类的构造函数吗? - Can constructor call another class's constructor in c++? 如何将 class 方法作为参数传递给另一个 function 并稍后调用它,最好使变量 class 方法签名显式? - How can I pass a class method as a parameter to another function and later call it, preferably making the variable class method signature explicit? 稍后在 C++ 中调用基类构造函数(不在初始化列表中) - Call a base class constructor later (not in the initializer list) in C++ C++ 将来自成员 class 属性的 function 存储在另一个 ZA2F2ED4F8EBC2CBB1DZ21A29DC40AB6 中 - C++ Store a function from a member class property in another class C ++从类到类的“传递”函数调用 - “Pass” function call from class to class, C++ C ++如何从具有一个参数的派生类构造函数调用具有两个参数的超类构造函数? - C++ How can I call superclass constructor with two parameters from derived class constructor with one parameter? 为什么我不能从C ++中该类的实例调用该类的构造函数? - Why can I not call my class's constructor from an instance of that class in C++? C ++函数在调用时崩溃(类构造函数) - C++ function crashes on call ( class constructor ) 在构造函数中调用类的函数。 C ++ - Call a class' function in the constructor. C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM