如何从自身内部调用函数对象?看来我不能使用 this
。示例:
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;
}
对我来说效果很好。 Live example.
关于c++ - 递归调用函数对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928321/