递归调用函数对象 [英] Recursively call a function object

查看:188
本文介绍了递归调用函数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从内部调用函数对象?似乎我不能使用。示例:

How do I call a function object from within itself? Seems I cannot use this. Example:

class factorial {
  public:
  int operator()(int n) {
    if (n == 0)
      return 1;
    return n * ??(n-1);
  }
};

我放在 ??

推荐答案

#include <iostream>

class factorial {
public:
  int operator()(int n) {
    if (n == 0)
      return 1;
    return n * (*this)(n-1);
  }
};

int main()
{
    std::cout << factorial()(5) << std::endl;
}

适用于我。 即时示例

这篇关于递归调用函数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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