Home arrow C++ arrow Page 2 - Who`s Afraid to Be Const Correct? Help Your Compiler Help You
C++

Who`s Afraid to Be Const Correct? Help Your Compiler Help You


In programs and commercial libraries that are not const-correct, you may find that const_cast is used. While a programmer may do this to force an application to do what he wants it to, it is never a good idea. Jun Nakamura explains why.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
August 30, 2005
TABLE OF CONTENTS:
  1. · Who`s Afraid to Be Const Correct? Help Your Compiler Help You
  2. · The Compiler is Your Friend
  3. · Why Use the Compiler to Catch Errors?
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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.


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