Gernot Frisch
Guest
Posts: n/a

 08-04-2004
Hi,

when Do I need the:

a a: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

Gianni Mariani
Guest
Posts: n/a

 08-04-2004
Gernot Frisch wrote:
> Hi,
>
> when Do I need the:
>
> a a: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;
}

Gernot Frisch
Guest
Posts: n/a

 08-04-2004
> I think this example might help clear things up a little.

Not completly

struct B;

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

A operator +(const B&);

>
> };
>
>
> struct B
> {
> };
>

versus this one??
> B operator + ( const A &, const B & );

Thank you,
Gernot

Karl Heinz Buchegger
Guest
Posts: n/a

 08-04-2004
Gernot Frisch wrote:
>
> Hi,
>
> when Do I need the:
>
> a a: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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

Gernot Frisch
Guest
Posts: n/a

 08-04-2004
> 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

Niels Dekker
Guest
Posts: n/a

 08-04-2004
> when Do I need the:
>
> a a: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:perator+=(const A&)
{
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