Home arrow C++ arrow Const Correctness in C++

Const Correctness in C++

One of the biggest advantages of programming in C++ is that you can do the same thing in a variety of ways. At least in theory the compiler lets you do this. Under these circumstances, the difference between a good programmer and a very good programmer lies in the methods they use to accomplish specific tasks. Coding with const correctness in mind is one of the recommended techniques that raises you to the level of a very good programmer. Itís that important to grasp this concept.

Author Info:
By: Gabor Bernat
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
October 28, 2008
  1. · Const Correctness in C++
  2. · Constant Variables
  3. · Const in Functions and in Classes
  4. · Conclusion

print this article

Const Correctness in C++
(Page 1 of 4 )

It may seem to be just another pain in the neck at first glance. You could say "oh yes; it's not enough that my boss just hit me with lots of coding standards, now I got another issue to watch out for." If you don't know what coding standards are and how they can help you, please read this article. It sums up briefly the idea behind them.

But let's get back to const correctness. Good and excellent programmers use it not because it's trendy; it's because it helps cut the debugging time of code to a minimum and brings issues closer to the compiler. Generally, in coding, it is recommended that you bring any mistakes or errors closer to the compiler. This way the compiler tells you instantly if something is wrong and you evade hours of debugging. For example, you might write in an "if" statement first the constant and only after it the variable:

if (1 == alfa) // instead of alfa == 1

This way, if you for some reason (maybe a bad day or a stressful boss) put only one equal sign, the compiler will warn you at compile time, and you can correct it right away without wasting countless minutes searching for the problem that  makes the function/program work, but not the way you were expecting it to. Const correctness can be perceived as a tool or trick in that sense.

If you are interested in investing the time to read more about const correctness,  I welcome you aboard. There are few if any prerequisite skills needed for understanding this technique or this article; maybe some basic C++ familiarity, and an understanding of the object oriented programming method as well.

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