我正在开发一个输入事件系统,我可以非常灵活地使用在没有轮询的情况下发生特定输入时要做什么。我正在使用 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/