Home arrow C++ arrow Page 3 - 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 - Const-Correctness
(Page 3 of 6 )

When we pass a function argument by reference, the function is allowed to manipulate and make changes to the object being referenced. Some would even argue that for this very reason it is safer to pass function arguments by value when you want to make sure the function doesn’t make any alterations to them. I prefer to use constant references instead.

By passing a const reference to a function, you forbid the function to make any changes to the object via that reference.  Now we can make our first alteration to the FindClosest function:

Enemy const& FindClosest(list<Enemy> const &enemies,
     Player const &player) {
   assert (!enemies.empty()); 

      list<Enemy>::const_iterator closest;
      float min_sqdistance = FLT_MAX;
     
      list<Enemy>::const_iterator nmeIt;
      for (nmeIt=enemies.begin(); nmeIt!=enemies.end(); nmeIt++)
      {
       Vector3 nme2ply=player.GetPos() – nmeIt->GetPos();
       float sqdist=nme2ply.squaredLength();
       if (sqdist < min_sqdistance)
       {
        closest=nmeIt;
        min_sqdistance=sqdist;
       }
      }
return *closest;
         }

This alteration saves the need to construct an Enemy object local to the function body, and also the need to construct a temporary object to carry the result over to the caller.


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-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials