首页 新闻 会员 周边

c++11 std::bind 如何绑定 一个类的重名函数

0
[待解决问题]

class Test{
public:
    void work(){
        cout << "in work " << endl;
    }
   
    void work(int x){
        cout << "x = " << x << endl;
    }
};

int main(){

  Test test;
  thread th(std::bind(&Test::work, &test));
  th.join();

  return 0;

}如上所示,在Test类中,含有两个work函数,函数名相同,但参数不同。我想在main函数中新建一个线程,线程函数为work函数(无参数的那个),请问如何做到?

如果我直接按照上面这样写,会提示找不到确切的函数错误(因为有两个同名的work)。

农民伯伯-Coding的主页 农民伯伯-Coding | 菜鸟二级 | 园豆:202
提问于:2015-11-05 16:41
< >
分享
所有回答(1)
0

在thread后面加个参数试试:

thread th(std::bind(&Test::work, &test),10);

Plato | 园豆:197 (初学一级) | 2016-01-01 10:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册