Home arrow C++ arrow Page 3 - Temporary Variables: Temporaries Are Not Necessarily Evil
C++

Temporary Variables: Temporaries Are Not Necessarily Evil


In earlier articles, we learned about the problems the passing of objects by value can cause in C++. Returning objects by value, on the other hand, is not necessarily evil. Jun Nakamura explains the finer points.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
October 03, 2005
TABLE OF CONTENTS:
  1. · Temporary Variables: Temporaries Are Not Necessarily Evil
  2. · Binding a reference to a temporary object
  3. · Death by Returned const Reference
  4. · It can be Good to Return by Value

print this article
SEARCH DEVARTICLES

Temporary Variables: Temporaries Are Not Necessarily Evil - Death by Returned const Reference
(Page 3 of 4 )

My previous article ended with a function declaration that demonstrated the possibility of crafting a reference to a temporary object in a rather cunning way. Let's give that function a body and another try. While it may be safe to bind a reference to a temporary object, you cannot do so blindly.

Base const& AnotherCallByReference(Base const &obj) {
 return obj;
}

Reusing the Base and Empty classes from the previous article, we gain more insight into the lifetime of temporary objects.

If you’ve read the previous article, you now know that it is possible for an object to be sliced when a function accepts parameters passed by value or by const reference. Here it is possible to pass an Empty object into the AnotherCallByReference function, because the Base class has a conversion constructor that accepts Base objects.

NoBase nobase_obj;
Base const& crashbase=AnotherCallByReference(nobase_obj);
Crashbase.foo();
(void)printf(“Done!\n”);

Now you see that the const reference returned, does not necessarily refer to the object originally passed in to the function!

NoBase object created
Base created from NoBaseobject
Base destroyed
Base::foo() called

The caller received a const reference to a temporary object, and when it tried to call function foo() on it, the temporary object had already been destroyed!


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials