通过在函数后写N个括号来调用函数N次

Xia*_*Jun 1 c++

我想实现以下内容:

我定义了一个函数.当我在函数后写N(),函数将被调用N次.

我举个例子:

#include <iostream>
using namespace std;

typedef void* (*c)();
typedef c (*b)();
typedef b (*a)();

a aaa()
{
    cout<<"Google"<<endl;

    return (a)aaa;
}

int main()
{
    aaa()()()();
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

然后输出是:

在此输入图像描述

还有其他方法可以实现吗?

fas*_*ked 6

使用仿函数很简单.

#include <iostream>

struct Function
{
   Function& operator()() {
      std::cout << "Google" << std::endl;
      return *this;
   }
};

int main()
{
   Function f;
   f()()()();
}
Run Code Online (Sandbox Code Playgroud)