473,406 Members | 2,633 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,406 software developers and data experts.

overloading operator+ question

Ook
This is a few ways I've seen to overload operator+. I can understand that
you would want to pass a reference to the function if you wanted to change
some of the data elements of the class, but in the first example it's being
passed as a constant. Is this done for performance purposes so that it
doesn't create a copy of the class and pass the copy to the function?

Why would you have it return a reference to the class as in example 1? I
would think that the third example would be what you want to do.

Class Zoot.....

Zoot& operator+ (const Zoot& zzz); passes read only reference to class

Zoot operator+ (Zoot zzz); // passes copy of clacc

Zoot operator+ (const Zoot& zzz); // returns class, not reference - is this
right?
Oct 11 '05 #1
2 1334
I'd want to do this, but generally my mileage varies anyway..

const zap& operator + (const zap& object);

I don't know how the cool people think about it, but for me the unary +
operator is just syntatic sugar to have symmetry with unary - operator.
$.02 and all that.

Usually I think, what kind of expression the operator would be part of
and how would the "built-in" types handle the parameters and return
value. In this case, I'm still trying to figure out why I would want to
assign to the object which might been returned if I chose to implement
this differently. Guess I'm too dumb to answer your question afterall.
That's just how I'd do it.. (oO?)

Oct 11 '05 #2
Ook wrote:
This is a few ways I've seen to overload operator+. I can understand that
you would want to pass a reference to the function if you wanted to change
some of the data elements of the class, but in the first example it's being
passed as a constant. Is this done for performance purposes so that it
doesn't create a copy of the class and pass the copy to the function?

Why would you have it return a reference to the class as in example 1? I
*I* would not, most likely.
would think that the third example would be what you want to do.

Class Zoot.....
class Zoot { ....
Zoot& operator+ (const Zoot& zzz); passes read only reference to class

Zoot operator+ (Zoot zzz); // passes copy of clacc

Zoot operator+ (const Zoot& zzz); // returns class, not reference - is this
right?


Actually, if operator+ doesn't change _either_ operand (and it shouldn't,
if you want to follow the convention), then neither is fully correct. You
should do

Zoot operator+ (const Zoot& zzz) const;

V
Oct 11 '05 #3

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

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.