Solved

+ operator

Posted on 2003-11-05
9
204 Views
Last Modified: 2010-04-02
I found myself overloading the + operator for the first time today for a class that I'm writing. But I quickly realized that there appears to be an inherent problem with overriding the + operator for user-defined types.

Consider:

myclass a = "hey";
myclass b = " there";
myclass c;

c = a + b;

This is equivalent to:

c.operator=( a.operator+( b ) );

The + operator allocates a new "myclass" object, initializes it to the sum of 'a' and 'b', and then returns it. Then that temporary object is assigned to 'c'. But no one ever deletes the temporary 'myclass' object that the + operator created!

What is the solution? How is this typically implemented?

0
Comment
Question by:daniel_bigham
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
The temporary is typically allocated by the compiler with automatic storage (on the stack). The compiler reclaims the space at its leisure, calling your destructor.

-bcl
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>But no one ever deletes the temporary 'myclass' object that the + operator created!

Um, the compiler does it :o)

To be serious, in the above scenario, one would define an approriate copy ctor and 'operator=()' and everything should be OK.
0
 
LVL 1

Author Comment

by:daniel_bigham
Comment Utility
Thing is, I allocate memory within the + operator as follows:

myclass* returnValue = new myclass();

...

return *returnValue;

So in fact, the compiler never does claim the value...

I guess that's my problem isn't it... I should do it like:

myclass returnValue;

...

return returnValue;

Is that my problem? :)
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
Yep. Return an instance. -bcl
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:bcladd
Comment Utility
That is an instance that gets copied into the return value of the function and then goes out of scope (automatic storage). That space IS reclaimed and then the temporary is reclaimed as well.

-bcl
0
 
LVL 1

Author Comment

by:daniel_bigham
Comment Utility
Aha! Maybe I'm not dreaming.

I changed my code so that the return value is allocated on the stack instead of the heap, and when I tried to compile it I got a warning about "returning a temporary value".

When I went ahead and bypassed the warning to run my program, it crashed.

0
 
LVL 1

Author Comment

by:daniel_bigham
Comment Utility
Oh... I'm returning a reference. Is that the problem?
0
 
LVL 11

Accepted Solution

by:
bcladd earned 250 total points
Comment Utility
You NEVER want to return a reference to a stack allocated object. That object will no longer exist (the destructor will have been called) by the time any other function gets access to the reference. The simple operators are usually implemented to return an object and assignment operators return a reference to this (return *this;).

-bcl
0
 
LVL 1

Author Comment

by:daniel_bigham
Comment Utility
Great! Thanks for all your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now