Solved

+ operator

Posted on 2003-11-05
9
207 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

856 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