int operator()(int n)行的含义是什么?
它看起来像一个功能,但它是什么?
class collatzer
{
private:
int counter = 0;
public:
int operator ()(int n)
{
if(n==1)
return counter;
else if(n%2==0) //if n is even
{
counter += 1;
return (*this)(n / 2);
}
else
{
counter += 1;
return (*this)(3 * n + 1);
}
}
int getCounter()
{
return counter;
}
};