本文介绍了通用成员函数指针帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个关于一般成员函数指针的问题。我想尝试实现类似以下问题



本质上,我想要做的是注册一个成员函数指针,接受一个通用的Event对象作为其参数,特定事件类型(例如,KeyboardEvent)。然后,在我的输入管理类,我想要能够做的是每当用户点击一个键,我可以创建一个KeyboardEvent对象,包含一些关于按键的数据,并调用我注册的所有成员函数指针用我的KeyboardEvent对象作为他们的参数的KeyboardEvent类型。



我一直在玩的boost绑定和boost函数,他们似乎使我做95%我想做,唯一的问题,我试图存储所有的成员函数指针在向量中,但他们都是不同的类型,所以我不能这样做。所以我将它们绑定到一个函数对象,当我第一次注册的方法与我的事件处理系统,期望我指定执行中的所有参数。我不必在这一点上的对象,因为对象是由未来的一些未知事件生成的。



对不起,如果这没有任何意义。 p>

解决方案

(这里有一个更直接的答案你的boost :: function / bind问题,没有所有的Boost.Signals2东西。 p>

在使用boost :: bind时,你似乎没有使用_1,_2等占位符。此示例说明如何使用它们:

  struct KeyboardEvent {char key; }; 

typedef boost :: function< void(KeyboardEvent)>键盘处理

struct Handler
{
void onKey(KeyboardEvent event){std :: cout< event.key;}
};

int main()
{
Handler handler1,handler2;
std :: vector< KeyboardHandler>处理程序;
handlers.push_back(boost :: bind(& Handler :: onKey,& handler1,_1));
handlers.push_back(boost :: bind(& Handler :: onKey,& handler2,_1));
KeyboardEvent事件;
event.key ='z';
handlers [0](event);
handlers [1](event);
}


Hey, I've got a question about general member function pointers. I'm trying to achieve something similar to the following question How to define a general member function pointer

Essentially what I want to be able to do is to register a member function pointer that accepts a generic Event object as its argument with a specific event type (for example, a KeyboardEvent). Then, in my input management class, what I want to be able to do is whenever the user hits a key, I can create a KeyboardEvent object that contains some data about the keypress, and call all of those member function pointers that I registered with the KeyboardEvent type with my KeyboardEvent object as their parameter.

I've been playing around with boost bind and boost function and they seem to enable me to do 95% of what I want to do, the only problem I am trying to store all of the member function pointers in a vector, but they're all of different types so I can't do that. So I bind them into a function object when I first register the method with my event handling system, which expects me to specify all of the arguments at this point in the execution. I don't have to objects at this point though as the objects are generated by some unknown event in the future.

Sorry if none of this makes any sense.

解决方案

(Here's a more direct answer to your boost::function/bind problem, without all the Boost.Signals2 stuff.)

It seems you're not using the _1, _2, etc. placeholders when using boost::bind. This example illustrates how you should use them:

struct KeyboardEvent { char key; };

typedef boost::function<void (KeyboardEvent)> KeyboardHandler;

struct Handler
{
    void onKey(KeyboardEvent event) {std::cout << event.key;}
};

int main()
{
    Handler handler1, handler2;
    std::vector<KeyboardHandler> handlers;
    handlers.push_back(boost::bind(&Handler::onKey, &handler1, _1));
    handlers.push_back(boost::bind(&Handler::onKey, &handler2, _1));
    KeyboardEvent event;
    event.key = 'z';
    handlers[0](event);
    handlers[1](event);
}

这篇关于通用成员函数指针帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:31