8

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);
  }
};

What do I place at ???

r.v
  • 4,697
  • 6
  • 35
  • 57
  • 8
    What about `(*this)`? – dyp Jul 29 '13 at 15:42
  • 4
    Also, `this->operator()(n-1);` – jrok Jul 29 '13 at 15:46
  • @Borgleader: He's following a functor pattern, which calls for a non-`static` `operator()()`. It's not always valuable to add modifiers such as `static` just because you can, for this particular version of this particular functor. – Ben Voigt Jul 29 '13 at 15:46
  • added `public:` per the comment. – r.v Jul 29 '13 at 15:58
  • Just a quick comment: on systems with 32-bit integers, you cannot use this implementation to calculate 14! because 14! is larger than the largest number representable in 32 bits. And on systems with 64-bit integers, 22! is as large as you'll be able to go. – Nik Bougalis Jul 29 '13 at 16:21
  • @NikBougalis, right. A simple int factorial works well for example purposes though. My actual use case was quite different. – r.v Jul 29 '13 at 16:54

6 Answers6

14
#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;
}

Works fine for me. Live example.

dyp
  • 38,334
  • 13
  • 112
  • 177
9

You can either use the name of the overloaded operator:

operator()(n-1);

or invoke the operator on the current object:

(*this)(n-1);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

As DyP mentioned, you can call (*this)(n-1). However, it's odd to read, so you'd be better off splitting it out into a seperate calculate_factoral method and calling that instead

Sean
  • 60,939
  • 11
  • 97
  • 136
  • 3
    If you want to give a nicer name to `*this`, then a local reference variable should be adequate. e.g. `factorial& recurse = *this; return n * recurse(n-1);` What you can't do is move it into a separate function named `factorial`, since that would have a naming conflict with a constructor. – Ben Voigt Jul 29 '13 at 15:47
  • @Ben - thanks, I missed that this class was also called factoral! – Sean Jul 29 '13 at 15:49
2

As several people have pointed out you can just use the (*this)(n - 1) syntax. But this syntax isn't exactly intuitive and a slightly better solution may be to factor out the actual implementation into another named method.

class factorial { 
public:
  int operator()(int n) {
    return impl(n);
  }
private:
  int impl(int n) { 
    // actual work here 
  }
};
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

You can either use explicit operator syntax:

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

Or dereference this:

class factorial {
  int operator()(int n) {
    if (n == 0)
      return 1;
    return n * (*this)(n-1);
  }
};
Alexey Biryukov
  • 649
  • 8
  • 15
0

factorial surely?

I only know Java and a recursive factorial can be written as:

public class Factorial{

    public int factorial(int n) {
        return (n > 1) ? n * factorial(n-1) : 1;
    }
}

I assume the same principle applies.