Functionality of void operator()()

前端 未结 4 609
一生所求
一生所求 2021-02-01 05:11

I am confused about the functionality of void operator()().

Could you tell me about that, for instance:

class background_task
{
public:

            


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 05:39

    You can overload the () operator to call your object as if it was a function:

    class A {
    public:
        void operator()(int x, int y) {
            // Do something
        }
    };
    
    A x;
    x(5, 3); // at this point operator () gets called
    

    So the first parentheses are always empty: this is the name of the function: operator(), the second parentheses might have parameters (as in my example), but they don't have to (as in your example).

    So to call this operator in your particular case you would do something like task().

提交回复
热议问题