Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading ! operator for factorial

Silver
Guest
 
Posts: n/a
#1: Jul 22 '05
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::operator !()

{

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;

}



lilburne
Guest
 
Posts: n/a
#2: Jul 22 '05

re: overloading ! operator for factorial




Silver wrote:
[color=blue]
> Hi everyone,
>
> I want to overload the ! operator, so that when I write this in main
> x![/color]

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

Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 22 '05

re: overloading ! operator for factorial


Silver wrote:[color=blue]
>
> Hi everyone,
>
> I want to overload the ! operator, so that when I write this in main
> x![/color]

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

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Silver
Guest
 
Posts: n/a
#4: Jul 22 '05

re: overloading ! operator for factorial


I now begin to understand the use of operator overloading.
Thanks.

"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3FCF3493.D53C4CB3@gascad.at...[color=blue]
> Silver wrote:[color=green]
> >
> > Hi everyone,
> >
> > I want to overload the ! operator, so that when I write this in main
> > x![/color]
>
> 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[/color]
does.[color=blue]
> This means: Try to implement operators in a way as you would implement[/color]
them[color=blue]
> 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;
> }
>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at[/color]


Stewart Gordon
Guest
 
Posts: n/a
#5: Jul 22 '05

re: overloading ! operator for factorial


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>[color=blue]
> 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.[/color]
<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.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Thomas Matthews
Guest
 
Posts: n/a
#6: Jul 22 '05

re: overloading ! operator for factorial


Stewart Gordon wrote:
[color=blue]
> 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>
>[color=green]
>> 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.[/color]
>
> <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.
>[/color]

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

Silver
Guest
 
Posts: n/a
#7: Jul 22 '05

re: overloading ! operator for factorial


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

long A::operator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::operator &(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[i]&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)



Ron Natalie
Guest
 
Posts: n/a
#8: Jul 22 '05

re: overloading ! operator for factorial



"Silver" <argytzak@med.auth.gr> wrote in message news:bqq9gp$6ao$1@nic.grnet.gr...
[color=blue]
> cout << "\na[" << i << "].n & b.n = " << a[i]&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)
>[/color]
<< binds tighter than binary-&.
What you wrote is parsed as
... ( ... << a[i]) &b );

You want to write
... << (a[i]&b);


Thomas Matthews
Guest
 
Posts: n/a
#9: Jul 22 '05

re: overloading ! operator for factorial


Silver wrote:[color=blue]
> Back with another question:
> Why doesn't this work?
>
> long A::operator &(const A& alpha) const
>
> {
>
> return this->n + alpha.n;
>
> }
>
> long A::operator &(const int& value) const
>
> {
>
> return this->n + value;
>
> }
>[/color]

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

long A::operator &(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

red floyd
Guest
 
Posts: n/a
#10: Jul 22 '05

re: overloading ! operator for factorial


Thomas Matthews wrote:
[color=blue]
> Silver wrote:
>[color=green]
>> Back with another question:
>> Why doesn't this work?
>>
>> long A::operator &(const A& alpha) const
>>
>> {
>>
>> return this->n + alpha.n;
>>
>> }
>>
>> long A::operator &(const int& value) const
>>
>> {
>>
>> return this->n + value;
>>
>> }
>>[/color]
>
> Just curious, why do you reference member variables
> using the "this" pointer when you can access them
> directly?
>
> long A::operator &(const int& value) const
> {
> return n + value;
> }
>
>[/color]

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.

Closed Thread


Similar C / C++ bytes