C++
  Home arrow C++ arrow Page 2 - Temporary Variables: Keep Your Values Clos...
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: Keep Your Values Close, and Your References and Pointers Even Closer
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-09-26

    Table of Contents:
  • Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
  • To pass or not to pass by value
  • Const-Correctness
  • Call optimization
  • Examining possible dangers
  • The cost of implicit conversion

  • 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: Keep Your Values Close, and Your References and Pointers Even Closer - To pass or not to pass by value


    (Page 2 of 6 )

    By default, function parameters are passed by value. This means that the compiler creates a copy of the original object that was passed to the function and introduces the copy to the function as its parameter. The easiest way to visualize this is to use the MyClass class from the previous article and try the following test code:

    void ByValue(MyClass obj) {
     (void)printf(“ByValue called..\n”);
    }

    /* in main() */
    MyClass obj(“myObject”);
    (void)printf(“Calling ‘ByValue’\n”);
    ByValue(obj);

    Clearly we can see the copy constructor being called to copy ‘myObject’ into the function parameter ‘obj’ in order to expose it to the function ‘ByValue’.

    myObject - Constructor called.
    Calling 'ByValue'
    unnamed object - Copy Constructor called.
    ByValue called..
    unnamed object - Destructor called.
    myObject - Destructor called.

    We know that constructors (whether they are copy constructors or not), assignment operators and destructors can be costly operations.

    Let's see what happens when we pass ‘MyClass’ by reference to the function:

    void ByReference(MyClass &obj) {
     (void)printf(“ByReference called..\n”);
    }

    /* in main() */
    MyClass obj(“myObject”);
    (void)printf(“Calling ‘ByReference’\n”);

    No copy constructors were called… most excellent!

    Calling 'ByReference'
    ByReference called..

    Just like returning an object by reference, passing one by reference to a function allows you to keep working with the object itself, instead of instanced copies. This is much more efficient, since no new objects have to be created nor destroyed.

    The only time it is less efficient to pass a reference instead of a value is when you are working with primitive values like integers, floats, booleans, and so on. In these cases a reference causes unnecessary overhead. References are almost always implemented like pointers; so passing a primitive value directly is more efficient than passing the address to that value.

    More C++ Articles
    More By J. Nakamura


       · Hi, great article. Quick question:I have a function which takes a reference to...
       · Hi, this is one of those things that comes pretty intuitively, but looking it up in...
     

    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 1 hosted by Hostway