Home arrow C++ arrow Page 4 - 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++ - Conclusion
(Page 4 of 4 )

If you read my article about Coding standards you should already know what benefits it brings to your programming "kitchen." Const correctness promises to offer at least as many benefits if it is done right. You can get a code where the modification of every type can be tracked effortlessly.

But think it over; don't use const everywhere just because you can do it. Use it only where you are absolutely sure that no modification will be made. Write functions as generally as you can; if the function can have a const overload, make it. However, be aware of local variables and don't start returning const references/pointers to them; local variables are destroyed.

Only one question remains to be answered: when is it best to start implementing const capabilities in a program? The most accurate answer would be "before you start writing a single line." The functions in your head should be born with const correctness. It can be hard at the beginning (especially to not abuse it), but once you get it, it will became second nature, and spare you countless hours of debugging.

If you are still reading these lines that means you have reached the end of this article. Now the challenge begins. Start up your compiler and start using the knowledge amassed here. It won't be a trivial task, but if you ever run into  problems you can ask the community at DevHardware Forums at any time.

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

- 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