我可以在C++中实现以下功能吗? 我想保留myInstance
变量未指定,直到调用回调方法,而不是将它包含在boost::bind
实例中。boost :: mem_fn的正确类型是什么?
MyClass *myInstance;
void call(/* boost::mem_fn */ callback)
{
// Somewhere in this function, the operator()() method
// is called on the callback instance
callback(myInstance);
}
call(boost::mem_fn(&MyClass::myMethod));
我明白boost::mem_fn
是返回一个特定对象的功能,但我不能确定它的类型。该文件说明未指定作为其返回类型。其中一个boost::bind
构造函数将此类型作为参数,但我无法在其实现中找到实际的声明。此外,我甚至允许存储我自己的这种特定类型的实例,或者这是严格用于boost函数吗?
“这是编译器似乎在做一些非常神奇的事情的情况之一。”确实... 因此,我不允许将'callback'作为类中的字段存储以供将来使用,而不需要对类进行模板化?那么,我可以在不声明其模板类型的情况下持有'boost :: bind'的实例吗?谢谢你的回答,马特! – Vortico