没有继承的 C++ 函数指针(回调)

标签 c++ events callback function-pointers

我正在开发一个输入事件系统,我可以非常灵活地使用在没有轮询的情况下发生特定输入时要做什么。我正在使用 SDL 收集输入,因此您会看到一些 SDL 事件。

我计划通过将一个事件 (Keypress)“绑定(bind)”到一个函数来完成这一切。我希望它适用于任何具有我的 InputManager 实例的类。

这是我现在拥有的 InputManger 类:

//InputManager.h

typedef void (*Callback)();
typedef std::unordered_map<SDL_Keycode, Callback> KeyCode;
typedef std::unordered_map<int, KeyCode> Binding;

class InputManager {
public:
    void bindInput(EventType eventType, SDL_Keycode key, Callback f);
    void updateInput(SDL_Event event);
private:
    Binding inputBindings;
};

...

//InputManager.cpp

void InputManager::bindInput(SDL_EventType eventType, SDL_Keycode key, Callback f)
{
    inputBindings[eventType][key] = f;
}

void InputManager::updateInput(SDL_Event event)
{
    while(SDL_PollEvent(&event)) {
        if (inputBindings[event.type][event.key.keysym.sym])
            //Exists call function here
    }
}

这是将键绑定(bind)到函数的示例:

//A.cpp

//This function will be called when the class is being initialized
void init(InputManager* m_InputManager) {
    m_InputManager->bindInput(SDL_KEYDOWN, keypress_A, &A::testFunction);
}

//This is the callback function when A is pressed
void testFunction() {
    //Do stuff
}

我的问题是它不会占用任何成员 (A)。实际的错误代码是 无法使用类型为“void (A::*)()”的右值初始化类型为“Callback”(也称为“void (*)()”)的参数

现在我将其解释为我必须(在 InputManager 中)指定它采用 (A::*)。问题是我希望给 InputManager 赋予任何类使用它的能力,而 InputManager 不应该访问任何其他类(在本例中为 A)。

我尝试过的:

我想到的第一个想法是模板。我对他们没有那么多经验,所以他们仍然可以成为答案,但我失败了。由于回调函数是在 typedef 中定义的,我无法直接应用模板。这是它最终的样子:

template <class T>
struct Callback {
    typedef void (T::*F)();
};

template <class T>
struct KeyCodeStruct {
    typedef std::unordered_map<SDL_Keycode, Callback<T>> KeyCode;
};

template <class T>
struct BindingStruct {
    typedef std::unordered_map<int, KeyCodeStruct<T>> Binding;
};

class InputManager {
public:
    template <class T>
    void bindInput(SDL_EventType eventType, SDL_Keycode key, Callback<T> f);
    void updateInput(SDL_Event event);
private:
    template <class T>
    BindingStruct<T> inputBindings; //This is the area where I loose my grip on a template.
    //^^^ Member 'inputBindings' declared as a template
};

在这个错误之后,我实际上是在将我可能不应该放入模板中的东西放在模板中,并做一些我根本不理解的事情。我想问问会更好。我已经阅读了很多关于函数指针的内容,也许答案很简单,但我似乎无法理解它,所以也许可以尝试用外行的术语为我定义它?

编辑:

此外,我应该补充一点,我计划在我的整个项目中分发我的 InputManager,我在标题中没有继承的原因是因为我阅读了一个解决方案,其中您有一个所有其他文件固有的基类。我真的不能在大型项目上这样做。如果那是解决问题的唯一方法,我可能最终会这样做,但我可能会坚持投票。

最佳答案

问题:

问题是 Callback被定义为指向返回 void 的函数的指针,并且您的绑定(bind)尝试正在使用 &A::testFunction这是一个指向类 A 的成员函数的指针。

无需进一步要求即可调用指向函数的指针。但是需要为正确类的对象x(或指向对象的指针)调用指向类的成员函数的指针:

(x.*f)();    // call member function with object of the right member class

模板方法的问题在于,您需要为绑定(bind)存储不仅是指向成员函数的指针,还需要存储指向类型 T 的对象的指针,以便您可以为对象调用该函数。

解决方案

有一个使用 <functional> 的优雅解决方案允许存储 function和一个绑定(bind)。这也适用于 binding在成员函数和合适类的对象之间。

这里是这个概念的一个小演示,您可以根据自己的需要对其进行调整:

class Test {     // function with a callback for demo of proof of concept 
public:
    void myfct() {
        cout << "Myfct !!\n";
    }
}; 

class Manager {    // Simplified manager
    std::function <void()> f;    // this is the trick !! 

public: 
    template <class T>
    void binding(T *object, void (T::*mf)()) {
        f = std::bind(mf, object);   // you bind the member function to the object it has to be used with 
    }
    void testcall() {     // Just for showing the proof of concept 
        f(); 
    }
};

然后你可以试试这个:

Manager man; 
Test tst; 

man.binding(&tst, &Test::myfct); 
man.testcall();

关于没有继承的 C++ 函数指针(回调),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963966/

相关文章:

c# - 在没有标准 (Obj sender, EventArgs args) 签名的情况下创建事件处理程序委托(delegate)有多错误?

javascript - 如何根据 RxJs 中的事件有条件地缓冲按键输入

c++ - 'auto a_class::f(...) -> T const' 在语法中是否有歧义?

c# - 为什么使用ASP.NET从另一个页面返回后无法激发Page_Load-ergo史诗般的尴尬:)

c++ - 对数组使用unique_ptr,如何在 Debug模式下查看每个值 "local"或 "watch"标签

c# - 从 C# 到 C++ 的回调函数

perl - 在 Perl 中,您如何动态选择要用作回调的方法?

node.js - Node-soap 客户端和回调范围

c++ - A*算法人工智能中魔方的启发式函数

c++ - 避免递归(?) #include 声明