Home arrow C++ arrow Page 2 - Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
C++

Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer


As you know, in programming C++, it is much better to return a reference to an object than it is to return that object by value. As we will see in this article, it is also much better to pass a function parameter by reference than it is to pass it by value. But there are exceptions to this, as Jun Nakamura explains.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
September 26, 2005
TABLE OF CONTENTS:
  1. · Temporary Variables: Keep Your Values Close, and Your References and Pointers Even Closer
  2. · To pass or not to pass by value
  3. · Const-Correctness
  4. · Call optimization
  5. · Examining possible dangers
  6. · The cost of implicit conversion

print this article
SEARCH DEVARTICLES

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.


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