我是C ++的新手。我学习了一个简单的例子(见下面的代码)。我不能理解重载“()”的目的是什么。 std::thread my_thread(my_func);
中使用了“重载()”吗?
感谢您的所有关注。
struct func
{
int& i;
func(int& i_) : i(i_) {}
**void operator() ()** // the question point
{
for (unsigned j=0 ; j<1000000 ; ++j)
{
do_something(i);
}
}
};
void oops()
{
int some_local_state=0;
func my_func(some_local_state);
std::thread my_thread(my_func);
my_thread.detach();
}
答案 0 :(得分:1)
使用func my_func(some_local_state);
,
你现在可以打电话给my_func();
了
是一个函数(而不是类似my_func.dosomething();
的东西)和循环
将执行操作员功能。
std::thread
以这种方式使用它(无论出于何种原因,创作者只是喜欢它)
因此,如果没有operator()
,您的对象对std::thread