Solved

+ operator

Posted on 2003-11-05
9
205 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
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9690044
Yep. Return an instance. -bcl
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

920 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

14 Experts available now in Live!

Get 1:1 Help Now