Solved

+ operator

Posted on 2003-11-05
9
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9689733
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
ID: 9689734
>>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
ID: 9690033
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:bcladd
ID: 9690044
Yep. Return an instance. -bcl
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9690054
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
ID: 9690082
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
ID: 9690088
Oh... I'm returning a reference. Is that the problem?
0
 
LVL 11

Accepted Solution

by:
bcladd earned 250 total points
ID: 9690174
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
ID: 9690283
Great! Thanks for all your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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