C++
  Home arrow C++ arrow Page 3 - Temporary Variables: Temporaries Are Not N...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Temporary Variables: Temporaries Are Not Necessarily Evil
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-10-03

    Table of Contents:
  • Temporary Variables: Temporaries Are Not Necessarily Evil
  • Binding a reference to a temporary object
  • Death by Returned const Reference
  • It can be Good to Return by Value

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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!

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT