C++
  Home arrow C++ arrow Page 2 - Who`s Afraid to Be Const Correct? Help You...
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  
Mobile Linux 
App Generation ROI 
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++

Who`s Afraid to Be Const Correct? Help Your Compiler Help You
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-08-30

    Table of Contents:
  • Who`s Afraid to Be Const Correct? Help Your Compiler Help You
  • The Compiler is Your Friend
  • Why Use the Compiler to Catch Errors?
  • Conclusion

  • 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


    Who`s Afraid to Be Const Correct? Help Your Compiler Help You - The Compiler is Your Friend


    (Page 2 of 4 )

    Maybe you haven’t given it much thought, but returning a const value from a function can help you catch errors. Imagine we have a Vector class and we would like to be able scale a vector object by multiplying it with a float:

    class Vector3 {

    public:

     ...

     Vector3 const operator * (float factor) const;

     ...

    };

    Because ‘operator *’ returns a const value, the compiler will prevent us from writing code like:

    (vectA * PI) = vectB;

    simply because you cannot assign ‘vectB’ to a constant value! The first thing you will think is that you would never write code like that, but bugs are introduced in silly ways… at least my bugs often turn out to be silly and laugh at me.

    Have you ever been bitten by this bug?

    if (valueA = false) { … }

    The compiler won’t complain because it is legal to write code like this:

    bool  bar( ) {

    bool result;

    if (!(result = foo()) ) { … }

    return result;

    }

    Therefore you will find experienced coders who have been bitten one time too many coding this instead:

    if (false == valueA) { … }

    Because the compiler does complain about:

    if (false = valueA) { … }

    Surely you agree the statement above makes no sense… same goes for:

    if ( (vectA * PI) = vectB ) { … }

    when you actually meant:

    if ( (vectA * PI) == vectB ) { … }

    Not everybody agrees on this though. John Lakos for example states that since returned values are copies anyway, exploiting the notion of a const versus a non-const temporary value is typically unnecessary and can interfere with template instantiation [Lakos].

    Personally I prefer the compiler to help me catch my mistakes. Temporary values (rvalues) are not trivial, and we will take a closer look at them in the next article.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT