小编add*_*ssh的帖子

为什么在这段代码中调用函数调用运算符?

我不明白为什么该语句test()()要调用函数调用运算符。

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

class test{
public:
    test(){
        cout<<"i am in constructor"<<endl;
    }
    void operator() (){
        cout<<"i am in an overloaded function"<<endl;
    }
};

int main()
{
   test t;
   test()();
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出 -

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

class test{
public:
    test(){
        cout<<"i am in constructor"<<endl;
    }
    void operator() (){
        cout<<"i am in an overloaded function"<<endl;
    }
};

int main()
{
   test t;
   test()();
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ loops operator-overloading c++11

1
推荐指数
1
解决办法
100
查看次数

标签 统计

c++ ×1

c++11 ×1

loops ×1

operator-overloading ×1