a+b+c is not equal to a+(b+c)

A

Adrian

Gunnar said:
Why would that be better? I thought the result would be the same? That is,
operator+(a,b) is the same as a+b.
Becuase that way you can write
1. a+5
2. 5+a
3. a+b

If operator+ is a friend a Bigint will automatically be constructed for
1 and 2 if needed. As a member function option 2 isn't possible


Adrian
 
P

Peter Koch Larsen

Gunnar G said:
Why would that be better? I thought the result would be the same? That is,
operator+(a,b) is the same as a+b.

You should in BigInt have an operator:

BigInt& operator+=(BigInt const& rhs);

and the same for -=, *= and /=.

Now the "standard" operators become very simple to write:

BigInt operator+(BigInt lhs,BigInt const& rhs)
{
return lhs+=rhs;
}

The same principle is used for the other operators.

Kind regards
Peter
 
H

Howard

Victor Bazarov said:
Howard said:
[..]
Is there anyplace that lists which operators should be friends and which
should be members? I always seem to end up looking at existing code
someplace else when trying to decide, and that's a big waste of time.

IIRC, it's called "Effective C++". Probably, Item 19. Don't have my copy
here, sorry.

Victor

Ok, I've re-read that section now. Still, I have trouble remembering it. I
was kind of hoping for a little chart for operators (only). I suppose I can
make one myself...

-Howard
 
V

Victor Bazarov

Howard said:
Howard said:
[..]
Is there anyplace that lists which operators should be friends and which
should be members? I always seem to end up looking at existing code
someplace else when trying to decide, and that's a big waste of time.

IIRC, it's called "Effective C++". Probably, Item 19. Don't have my copy
here, sorry.

Victor


Ok, I've re-read that section now. Still, I have trouble remembering it. I
was kind of hoping for a little chart for operators (only). I suppose I can
make one myself...

It's not a bad idea. You'll learn it better while making it than if
somebody else makes one and you just read it.

V
 

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,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top