提高::绑定和放大器;提振::函数指针超载或模板的成员函数 [英] boost::bind & boost::function pointers to overloaded or templated member functions

查看:151
本文介绍了提高::绑定和放大器;提振::函数指针超载或模板的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回调的机制,涉及到的类是:

I have a callback mechanism, the classes involved are:

class App
{
    void onEvent(const MyEvent& event);
    void onEvent(const MyOtherEvent& event);

    Connector connect;
}

class Connector
{  
   template <class T> void Subscribe(boost::function <void (const T&)> callback);
}

App::App()
{
    connect.Subscribe<MyEvent>(&App::OnEvent<MyEvent>);
}

首先这个code不能编译,这是一个例证。

First off this code doesn't compile, it's an illustration.

使用模板复杂化我的例子,但我离开了他们,因为它影响了我的问题。这似乎肯定我,我的订阅需要被模板化,因为连接器​​类不知道有多少事件类型处理。当我尝试创建一个:

The use of templates complicates my example, but I left them in because its affecting my problem. It seems certain to me that my subscribe needs to be templated because the Connector class doesn't know how many event types it handles. When I try to create a:

boost::function f = &App::OnEvent,

我试图创造一个OnEvent为模板功能,具有专业,但似乎编译器处理我的OnEvent充当过载,而不是模板特,否则我得到的模板特没有命名空间的错误,如果我尝试显式声明为

I tried creating OnEvent as a template function, with specializations, but it seems that the compiler is treating my OnEvent functions as overloads rather than template specializations, or else I get the template specialization not in namespace error if I try to explicitly declare it as

template <> OnEvent(const MyEvent& e) ...

我可以得到下面的编译:

I can get the following to compile:

boost::function <void (App*, const MyEvent&)> f = &App::OnEvent;
f(this, e);

这编译,运行,和作品。

That compiles, runs, and works.

boost::function<void (const MyEvent&)> g = boost::bind(&App::OnEvent, this);

没有。我认为它是因为我没有正确指定一个重载函数的地址。

does not. I think its because I'm not correctly specifying the address of an overloaded function.

现在在解释这一切的泰迪熊 - ?如何正确地创建一个函数指针的重载或模板成员函数和this指针绑定到它:我认为我的问题是,

Having now explained all this to the teddy bear - I think that my question is "How do I correctly create a function pointer to an overloaded or templated member function and bind the this pointer to it?"

推荐答案

我认为你需要消除歧义的重载函数的地址。您可以通过显式铸造函数指针一个具有正确的参数做到这一点。

I think you need to disambiguate the address of the overloaded function. You can do this by explicitly casting the function pointer to the one with the correct parameters.

boost::bind( static_cast<void (App::*)( MyEvent& )>(&App::OnEvent) , this, _1);

类似的问题+对gamedev.net 解决方案

这篇关于提高::绑定和放大器;提振::函数指针超载或模板的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆