Solved

+ operator

Posted on 2003-11-05
9
210 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
Technology Partners: 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

Technology Partners: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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