473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

operator overloading...

Hi,

when Do I need the:

a a::operator+(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
Jul 22 '05 #1
5 1061
Gernot Frisch wrote:
Hi,

when Do I need the:

a a::operator+(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;
}
Jul 22 '05 #2
> I think this example might help clear things up a little.

Not completly
struct B;
struct A
{
A operator + ( const A & );
// What about:
A operator +(const B&);

};
struct B
{
};

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


Thank you,
Gernot
Jul 22 '05 #3
Gernot Frisch wrote:

Hi,

when Do I need the:

a a::operator+(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
kb******@gascad.at
Jul 22 '05 #4
> 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
Jul 22 '05 #5
Gernot Frisch asked:
when Do I need the:

a a::operator+(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::operator+=(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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.