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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway