简体   繁体   English

C ++中的operator()()有什么作用?

[英]What does operator()() in c++ do?

I'm new to C++11 thread , when reading a tutorial , I see a piece of code like this. 我是C ++ 11线程的新手,在阅读教程时,我看到了这样的一段代码。

 #include <thread>
 #include <iostream>
 using namespace std;

 class background_task
 {
  public:
     void operator()() const
     {
         cout<<"This is a new thread";
     }
 };

int main()
{
   background_task f;
   std::thread my_thread(f);
   my_thread.join();
}

The output will be "This is new thread", but i don' really understand what does the function "operator()() const" mean?. 输出将是“ This is new thread”,但是我真的不明白函数“ operator()()const”是什么意思。 In this case, it acts really the same with the constructor, is it right?. 在这种情况下,它与构造函数的作用完全相同,对吗?

And how can C++ have a syntax like that? C ++如何拥有这样的语法? I have search about related topic by using the search engine but no found no result. 我已经使用搜索引擎搜索了相关主题,但是没有找到结果。

Thanks in advanced. 提前致谢。

void operator()() means an instance of the class with that operator can be called with function call syntax, with no return value, and without any parameters. void operator()()意味着可以使用函数调用语法来调用具有该操作符的类的实例,而无需返回值且没有任何参数。 For example: 例如:

background_task b;

b(); // prints "This is a new thread"

The operator() part indicates it is a call operator, the second set of empty parentheses () indicate the operator has no parameters. operator()部分表示它是一个调用运算符,第二组空括号()表示该运算符没有参数。 Here is an example with two parameters and a return value: 这是一个带有两个参数和一个返回值的示例:

struct add
{
  int operator()(int a, int b) const { return a + b; }
};

add a;
int c = a(1, 2); // c initialized to 1+2

Note that this syntax pre-dates C++11. 请注意,此语法早于C ++ 11。 You can create callable types (also referred to as functors ) in C++03. 您可以在C ++ 03中创建可调用类型(也称为函子 )。 The connection with C++11 is that the std::thread constructor expects something that can be called without arguments . 与C ++ 11的联系在于std::thread构造函数期望可以不带参数调用的东西。 This could be a plain function 这可能是一个普通的功能

void foo() {}

a static member function 静态成员函数

struct foo {
  static void bar() {}
};

an instance of a type such as background_task , a suitable lambda expression, a suitable invocation of std::bind , in short, anything that can be called without arguments. 一个类型的实例,例如background_task ,一个合适的lambda表达式,一个合适的std::bind调用,简而言之,是任何可以不带参数调用的东西。

It's just operator overloading and has nothing to do with C++11 or multi-threading. 它只是运算符重载,与C ++ 11或多线程无关。 An overloaded operator is just a normal function with a funny name (this may be a bit oversimplified, but it's a good rule of thumb for beginners). 重载的运算符只是一个带有有趣名称的普通函数(这可能有点过分简化,但这对初学者来说是一个很好的经验法则)。

Your class has a function named () . 您的课程有一个名为()函数 That's all. 就这样。 Technically , you could as well have named the function foo or f or TwoParentheses . 从技术上讲 ,您也可以将函数命名为foofTwoParentheses

Consider a simpler example: 考虑一个更简单的示例:

#include <iostream>

class Example
{
public:
    void operator()() { std::cout << "()"; }
    void foo() { std::cout << "foo"; }
    void TwoParentheses() { std::cout << "TwoParentheses"; }
};

int main()
{
    Example e;
    e.operator()();
    e.foo();
    e.TwoParentheses();
}

Now calling an overloaded operator like in this example in main , spelling out the entire .operator() part, is pretty pointless, because an overloaded operator's purpose is to make the calling code simpler. 现在像在main中的此示例中那样调用重载运算符,拼出整个.operator()部分是毫无意义的,因为重载运算符的目的是使调用代码更简单。 You would instead invoke your function like this: 您可以这样调用函数:

int main()
{
    Example e;
    e();
}

As you can see, e(); 如您所见, e(); now looks exactly as if you called a function. 现在看起来就像您调用了函数一样。

This is why operator() is a special name, after all. 毕竟,这就是为什么operator()是一个特殊名称的原因。 In a template , you can handle objects with operator() and function pointers with the same syntax . 模板中 ,您可以使用operator()和具有相同语法的函数指针来处理对象。

Consider this: 考虑一下:

#include <iostream>

class Example
{
 public:
    void operator()() { std::cout << "Example.operator()\n"; }
};

void function() { std::cout << "Function\n"; }

template <class Operation>
void t(Operation o)
{
    o(); // operator() or "real" function
}

int main()
{
    Example object;
    t(object);
    t(function);
}

This is the reason why operator() is an important function in C++ generic programming, and is often required. 这就是为什么operator()在C ++泛型编程中是重要函数并且经常需要的原因。

It has nothing to do with C++11, it's the function call overload operator. 它与C ++ 11无关,它是函数调用重载运算符。 That means if you have a class like yours, you can create an instance of it and use as a function: 这意味着,如果您有一个像您这样的类,则可以创建它的实例并用作函数:

int main()
{
    background_task bt;
    bt();
}

The above main function should give the same result as your simple thread example. 上面的main函数应产生与您的简单线程示例相同的结果。

it is operator over loading. 是操作员超负荷的。 the user provide an additional use to () operator. 用户为()运算符提供了额外的用途。 Example for static polymorphism. 静态多态的示例。 it is fearture of Object orieted program 它是Object Orieted程序的恐惧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM