Home arrow C++ arrow Page 4 - 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 - Call optimization
(Page 4 of 6 )

Now let’s take a better look at how we’d use this function:

Enemy activeEnemy = FindClosest(enemyList, player);

The first and simplest optimization that comes to mind is the application of a copy constructor. With the statement above, an object must be created by calling its constructor (first call), after which an object is assigned to it by calling its assignment operator (second call). If a copy constructor is used, only one call to that copy constructor has to be made:

Enemy activeEnemy( FindClosest(enemyList, player) );

Compilers can optimize this statement by replacing the call to the constructor and assignment operator with that of the copy constructor… but there are no guarantees.

Instead of copying the data of the returned enemy into another Enemy object, we could use the const reference the function is returning:

Enemy const &activeEnemy = FindClosest(enemyList, player);

The obvious downside of a const reference is that we cannot make changes to the referenced object, and cannot use any of its functions unless they are declared const.

If we need to change the state of an enemy, we will have to return a non-const reference. To be able to return a non-const reference, we’ll have to pass a non-const reference to the enemy list to the function as well. The reference to the player object can remain const since we don’t expect a function that works with enemies to make changes to the player object.

Finally, here is a function declaration that is usable:

Enemy& FindClosest(list<Enemy> &enemies, Player const &player);

The enemy list is no longer const, so we can replace the const_iterators in the function body with regular iterators again.


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