# Factorial operator !

This is a discussion on Factorial operator ! within the C++ Programming forums, part of the General Programming Boards category; I'm trying to overload a factorial operator, and I'm caught on recursion. Here is my code so far: Code: int ...

1. ## Factorial operator !

I'm trying to overload a factorial operator, and I'm caught on recursion. Here is my code so far:
Code:
```int operator!(int number);
...
int operator!(int number)
{
int temp;
temp = number*((number-1)!)
return temp;
}```
Is there any way I can use ! in the definition? Also, if I define a simple operator, it will not compile, giving the error: 'int operator!(int)' must have an argument of class or enumerated type. I redefined ! to simply multiply the number by itself. (number*number). Can anyone give me some pointers?

2. You can't redefine operators for basic types.

You need to create your own integer-like class to define your own factoral opperator.

3. from the C++ standard (13.5.6)
An operator function shall either be a non-static member function or be a non-member function and have at
least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

4. Thanks, I've rewrote it but I still need an aswer to my recursive question.

5. Recursion is a form of a loop. A loop generally has a termination condition. The recursion needs this as well. Perhaps when you've reached zero?

6. you just need an end case.
since 1! = 1 just check for that
Code:
```if (number == 1)
return 1;```
btw you might want to consider what happens if you input a negative number?

7. And you need to check for 0 factorial; 0 factorial equals 1.

8. Sorry, copying error. Here is my current code:
Code:
```factorial operator!(factorial thenum)
{
factorial temp;
if(thenum.number <= 0)
return 1;
temp.number = number.number*((number.number-1)!)
return temp.number;
}```
This still won't compile, and when I try to use the operator it tells me there should be a ; before it, aka this
Code:
`3!`
would become this:
Code:
`3;!`
And of course that won't compile

9. The ! operator is a prefix operator. The only postfix operator in C++ is postfix ++ and --.

Code:
```    if(thenum.number <= 0)
return 1;```
Code:
```    if(thenum.number <= 1)
return 1;```

11. I think the ! operator normally goes in front of a variable. I'm not even sure there is any way to make it a postfix operator.

And to calculate a factorial, I would prefer a non-recursive loop. Set result = 1, then multiply it with all values from 2 to n to get n!

Code:
```....
int operator!(const MyInt a) //non-recursive
{
int result = 1;
for (int i = 2; i <= a.value; i++)
result *= i;
return result;
}

int main()
{
MyInt test(5);
std::cout << !test << std::endl; //NB, prefix
std::cin.get();
}```

12. btw you can calculate the factorial using template meta programming for compile-time constants.

Code:
```template <unsigned int Input>
unsigned int Factorial(void)
{
return Input * Factorial<Input - 1>();
}

template <>
unsigned int Factorial<1>(void)
{
return 1;
}

template <>
unsigned int Factorial<0>(void)
{
return 1;
}```
no run-time code at all! doesn't work for every situation, but still a useful trick.

13. I cringe at the possibility of using ! as factorial operator. Even if just for some specific class. It goes against everything an operator overload should be. Not to mention it's not even possible to mimic the mathematical semantics. So there isn't even that to hold on as a pro argument.

ChaosEngine solution is the absolute best.

14. >> ChaosEngine solution is the absolute best.

thanks, but unfortunately my solution doesn't work for non-compile time constants.
Code:
```unsigned int someNum = 0;