operator overloading...

G

Gernot Frisch

Hi,

when Do I need the:

a a::eek:perator+(a _1);
and when the
a operator+(a _1, a _2)

??



--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
G

Gianni Mariani

Gernot said:
Hi,

when Do I need the:

a a::eek:perator+(a _1);

This is a member function
and when the
a operator+(a _1, a _2)

This is a non-member function.

I think this example might help clear things up a little.

struct A
{
A operator + ( const A & );

};


struct B
{
};

B operator + ( const B &, const B & );
B operator + ( const B &, const A & );
B operator + ( const A &, const B & );

void foo()
{

B b1;
B b2;
A a1;
A a2;

b1 = b1 + b2;
b1 = a1 + b1;
b1 = b1 + a1;
a1 = a1 + a2;
}
 
K

Karl Heinz Buchegger

Gernot said:
Hi,

when Do I need the:

a a::eek:perator+(a _1);
and when the
a operator+(a _1, a _2)

??

The first is a member function, while the second is not.

The difference is as follows:

Assume

class A
{
public:
A( int i ) : m_i( i ) {}

private:
int m_i;
}

That is: there is a class where objects can implicitely be constructed
from an int.

Now assume you want to write an op+ to add 2 A objects. You do this
as member function

class A
{
....

A operator+( const A& Arg ) { .... }
};

Now what can you do with it?

You can eg. write

A ObjA( 5 );
A ObjB( 7 );
A Result;


Result = ObjA + ObjB;

No problem. But you can also write

Result = ObjA + 7;

Why? Because the compiler can use the constructor to first convert 7
into an A object and then use op+ to perform the addition.

But can you also write

Result = 7 + ObjA;

And the answer is: No. Because 7 is an int, and there is no op+ for
an int which takes an A object.

This seems illogical and it can be cured by making op+ a non member
function

A operator+( const A& lhs, const A& rhs ) { ... }

Now the compiler can use this operator as long as there is one A
object on either side of the '+'. The other one will be converted
to an A object if necessary.
 
G

Gernot Frisch

This seems illogical and it can be cured by making op+ a non member
function

A operator+( const A& lhs, const A& rhs ) { ... }

Now the compiler can use this operator as long as there is one A
object on either side of the '+'. The other one will be converted
to an A object if necessary.

Now, I toally got this! Thank you a lot for you explaination.
-Gernot
 
N

Niels Dekker

Gernot Frisch asked:
when Do I need the:

a a::eek:perator+(a _1);
and when the
a operator+(a _1, a _2)

Karl Heinz Buchegger replied:
The first is a member function, while the second is not. ....
A operator+( const A& lhs, const A& rhs ) { ... }

It has been suggested to implement operator+ in terms of member
operator+= (e.g., last Friday by Richard Herring, on this newsgroup).
Of course, you should have operator+= as follows:
A& A::eek:perator+=(const A&)
{
// TODO: The addition itself!
return *this;
}

If you do, is there a difference between the following two versions?

A operator+(const A& _1, const A& _2)
{
return A(_1) += _2;
}

and:

A operator+(A _1, const A& _2)
{
return _1 += _2;
}

Which one is preferable? Will it make any difference to the caller of
operator+?


Niels Dekker
www.xs4all.nl/~nd/dekkerware
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top