Home arrow C++ arrow Page 6 - Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
C++

Who`s Afraid to Be Const Correct? Reading Const Correctly in C++


You can do more with the const keyword in C++ than you can in C. Many programmers avoid using it, however, in part because it can be a little tricky to understand at first. Jun Nakamura explains how to use the keyword, and the excellent effect it can have on your code.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
August 16, 2005
TABLE OF CONTENTS:
  1. · Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
  2. · Const Declarations
  3. · The Constant Value vs. La Valeur Constante
  4. · Syntactical Substitution Problems
  5. · Const Member Functions
  6. · Right There Right Now

print this article
SEARCH DEVARTICLES

Who`s Afraid to Be Const Correct? Reading Const Correctly in C++ - Right There Right Now
(Page 6 of 6 )

Don’t be tempted to implement const-correctness at a later stage when you can do it right now. Making a parameter const in a function declaration can have a cascading effect. It is very likely for variables and objects to be passed on from function to function, and a stricter contract near the top of the call-stack can have deep-reaching consequences.

When you have these functions:

void foo(int &val) { bar(val); }

void bar(int &val) { g(val); }

void g(int &val) { … }

Changing the non-const reference parameter ‘val’ to a const reference will affect the other two functions as well. When you are in full production… it will only be the beginning of a long and painful code correction.

What better day to start than today. Delay will only make it more painful, and not doing it won’t help you write better code. 

References

[K&R] – Kernighan and Ritchie

“The C Programming Language” – ISBN 0131103628

[Meyers] – Scott Meyers

“Effective C++” – ISBN 0201924889

item 20 ‘Avoid data members in the public interface’

item 21 ‘Use const whenever possible’

[Sutter] – Herb Sutter

“Exceptional C++” -  ISBN 0201615622


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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