我想使用boost创建一个新线程,函数RenderOut是BlueOut类的成员。
该函数已重载,如何在将其传递给Boost线程时解决此歧义。
void BlueOut::RenderOut( int i);
void BlueOut::RenderOut( std::string str);
BlueOut blueout;
boost::thread* thr = new boost::thread(boost::bind ( &BlueOut::RenderOut , &blueout ));
要从重载集中选择成员函数,请将成员函数指针强制转换为所需的类型。因此要选择int
版本,请使用以下命令:
static_cast<void (BlueOut::*)(int)>(&BlueOut::RenderOut);
要选择std::string
版本,请使用以下命令:
static_cast<void (BlueOut::*)(std::string)>(&BlueOut::RenderOut);
在示例中使用此代码,您将编写以下代码:
// you didn't specify which overload you wanted so I chose `int` because I have no idea
boost::thread* thr = new boost::thread(
boost::bind(
static_cast<void (BlueOut::*)(int)>(&BlueOut::RenderOut),
&blueout
)
);
相同的原理也适用于自由函数和静态函数。鉴于以下功能:
void RenderOut(int);
void RenderOut(std::string);
您可以选择以下int
版本:
static_cast<void(*)(int)>(&RenderOut);
...并选择std::string
与此版本:
static_cast<void(*)(std::string)>(&RenderOut);
您可以在cppreference上阅读有关此内容的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句