overloading ! operator for factorial

S

Silver

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!
I can get the factorial of x.
The problem is, that the operator I want to overload takes no parameter.
Most examples I'ver seen, have a parameter. I wonder if this causes the
problem.
My code is

float A::eek:perator !()

{

float product = 0.0;


if (n != 0)

{

for ( ; n > 0 ; n--) // We omit the initialization expression

product *= n; // since we have initialized n with the constuctor


return product;

}

else

return 1.0;

}
 
L

lilburne

Silver said:
Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!

How is that going to work? operator! applies to the argument to the
right of it.
 
K

Karl Heinz Buchegger

Silver said:
Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!

That's not possible. The best you can get would be
!x

But it still is not a good idea. The reason is: In C++ all the standard
operators have a predefined meaning, in the case of ! this is 'not'.
So when reading through a source code, everybody seeing the line:

i = !x;

thinks immediatly: i gets assigned 'not x' and not i gets assigned the
factorial of x.

There is a simple rule when it comes to operator overloading: Do as int does.
This means: Try to implement operators in a way as you would implement them
for int (if you could). In principle you can create a class, which has
an operator+ which does the equivalent of multiplication. But this would
be confusing, cause everybody reading a + b thinks of addition and not
of multiplication. That's what is ment with: Do as int does.

Anyway:

#include <iostream>
using namespace std;

class A
{
public:
A( int n ) : m_n( n ) {}
double operator!() const {
double Result = 1.0;
for( int i = 1; i <= m_n; ++i )
Result *= i;
return Result;
}
private:
int m_n;
};

int main()
{
cout << !A(5) << endl;

A Test(8);
cout << !Test << endl;
}
 
S

Stewart Gordon

While it was 4/12/03 1:20 pm throughout the UK, Karl Heinz Buchegger
sprinkled little black dots on a white screen, and they fell thus:

There is a simple rule when it comes to operator overloading: Do as
int does. This means: Try to implement operators in a way as you
would implement them for int (if you could). In principle you can
create a class, which has an operator+ which does the equivalent of
multiplication. But this would be confusing, cause everybody reading
a + b thinks of addition and not of multiplication. That's what is
ment with: Do as int does.
<snip>

That makes sense. Shame someone didn't invent an operator that means
concatenation, leaving std::string stuck with +. (My hat goes off (not
that I ever have any reason to wear one) to D, which has introduced ~ as
a binary op for this purpose.)

A related question is what operator should be overloaded to represent
the dot product of vectors, particularly if you want to implement cross
product as well. I've been using % for this, for want of a better
option....

Stewart.
 
T

Thomas Matthews

Stewart said:
While it was 4/12/03 1:20 pm throughout the UK, Karl Heinz Buchegger
sprinkled little black dots on a white screen, and they fell thus:



<snip>

That makes sense. Shame someone didn't invent an operator that means
concatenation, leaving std::string stuck with +. (My hat goes off (not
that I ever have any reason to wear one) to D, which has introduced ~ as
a binary op for this purpose.)

A related question is what operator should be overloaded to represent
the dot product of vectors, particularly if you want to implement cross
product as well. I've been using % for this, for want of a better
option....

Stewart.

I prefer the FORTRAN concept: create explicit functions when no
operator exists. So given three vectors, the dot product becomes:
result = dot_product(vector_a, vector_b);
Rather than
result = vector_a % vector_b;
or
result = vector_a . vector_b;
or
result = vector_a * vector_b;
or
result = vector_a *. vector_b;

This issue seems to be one of typing rather than readability
or clarity.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
S

Silver

Back with another question:
Why doesn't this work?

long A::eek:perator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::eek:perator &(const int& value) const

{

return this->n + value;

}

NOTES: A is the name of my class. n is a private variable of the class.

...............

.............

cout << "\na[" << i << "].n & b.n = " << a&b;

NOTES: I have an array of A objects (that is a[]) and b is another A object



I get this error message when compiling with MS visual c++ .NET

error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'A' (or there is no acceptable conversion)
 
R

Ron Natalie

Silver said:
cout << "\na[" << i << "].n & b.n = " << a&b;

NOTES: I have an array of A objects (that is a[]) and b is another A object

I get this error message when compiling with MS visual c++ .NET

error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'A' (or there is no acceptable conversion)

<< binds tighter than binary-&.
What you wrote is parsed as
... ( ... << a) &b );

You want to write
... << (a&b);
 
T

Thomas Matthews

Silver said:
Back with another question:
Why doesn't this work?

long A::eek:perator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::eek:perator &(const int& value) const

{

return this->n + value;

}

Just curious, why do you reference member variables
using the "this" pointer when you can access them
directly?

long A::eek:perator &(const int& value) const
{
return n + value;
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

red floyd

Thomas said:
Just curious, why do you reference member variables
using the "this" pointer when you can access them
directly?

long A::eek:perator &(const int& value) const
{
return n + value;
}

May be an artifact of his IDE. A buddy of mine does that because if he uses "this->", he gets a drop down list of members.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top