Home arrow C++ arrow Page 2 - Who`s Afraid to Be Const Correct? Reading Const Correctly in 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
  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

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

The const keyword can be used inside classes for static/non static data members and function members (and their respective arguments). Outside classes it can be used for global or namespace constants, function arguments and for static variables (these are variables that are for the private use of the functions in their respective source files, and are not meant to be accessed by anything else [K&R]).  For pointers and references you can specify whether the pointer or reference itself is const, the data pointed to or referenced is const… or both.

Constants may also help you to get rid of ‘magic values’ in your code. This means that instead of ‘char msg[128];’ you can write more readable declarations like ‘char msg[MAX_MSG_LEN];’. The first declaration uses a ‘magic value’ because it is not immediately clear what 128 stands for (it just magically appears). On the other hand ‘MAX_MSG_LEN’ can be read and understood to represent the maximum message length.

An additional benefit of using constants is that you can group them together at the beginning of your source code and reuse them whenever possible (‘MAX_MSG_LEN’ can easily be used to declare an array more than once in your code!).

Please don’t declare MAX_MSG_LEN this way:

#define MAX_MSG_LEN 128

It works (the preprocessor replaces MAX_MSG_LEN with 128 before your code is seen by the compiler) but #defines are very hard (and clumsy) to debug… and they are not typesafe!

Instead use a declaration like this:

int const MAX_MSG_LEN = 128;

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials