void operator()的功能 [英] Functionality of void operator()()

查看:127
本文介绍了void operator()的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 void operator()()的功能感到困惑。

例如:

class background_task
{
public:

    void operator()() const
    {
        do_something();
        do_something_else();
    }
};

background_task f;

std::thread my_thread(f);

这里,为什么我们需要 operator c>?第一个和第二个()的含义是什么?实际上,我知道正常操作符的操作,但是这个操作符是混乱的。

Here, why we need operator()()? What is the meaning of the first and second ()? Actually, I know the operation of normal operator, but this operator is confusing.

推荐答案

第一个是运算符的名称 - 它是在对象上使用()时调用的运算符。第二个()是参数的,其中没有。

The first () is the name of the operator - it's the operator that is invoked when you use () on the object. The second () is for the parameters, of which there are none.

这是一个例子,使用它:

Here's an example of how you would use it:

background_task task;
task();  // calls background_task::operator()

这篇关于void operator()的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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