473,804 Members | 3,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1360
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
10459
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. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone in this news group might know the answer. Overloading the Operator Say I want to develop a...
2
5887
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 overloading. I just cannot find any explanation that clearly point out what the parts of the statement refer to. For example the book says: comp operator+(comp b)
4
6490
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. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
2
1808
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
5
5250
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 discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
2
2070
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 overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
1
1342
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 stand-alone helper function. If Integer's constructor were not declared as explicit, the previous would have one more possible translation which is operator+(i, Integer(5))
7
1833
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 xydata with operator- overloaded: class xydata { public:
5
2491
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
3634
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: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.