Home arrow C++ arrow Page 5 - Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally

Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally

This is the second article in a tutorial series covering const correctness. In this article, Jun Nakamura discusses some problems concerned with bitwise copy construction and bitwise constness, how to make your interface conceptually const, and how to make changes to an object that are not visible to the user.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
August 23, 2005
  1. · Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally
  2. · Bitwise Copy Construction
  3. · Bitwise Constness
  4. · Make Your Interface Conceptually Const.
  5. · Const and Mutable

print this article

Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally - Const and Mutable
(Page 5 of 5 )

Conceptually we can also reason that const should guarantee the user that no changes were made to the object that are visible to the user (when you are not caught stealing, is it a crime? When a tree falls in the forest does it make a sound?). If we consider MyClass again that wraps *m_pLabel, we can easily add a function that returns the length of that label:

class MyClass {



 size_t  LabelLength() const { return strlen(m_pLabel); }



Instead of calculating the length of the label every time the function is called, we could cache its length whenever it is calculated… and let’s assume that we only want to calculate its length when this is requested.

This is what MyClass could look like:

class MyClass {


 // ctor makes m_pLabel point to a copy of label MyClass(char const *label)

  : m_LabelLength(-1)


  m_pLabel=new char[MAX_LABEL_LEN];




~MyClass() {

 try { delete m_pLabel; }

 catch (...) { }



 char const* GetLabel() const { return m_pLabel; }


 void SetLabel(char const *label) {

  if (0 != label)


assert( (int)(strlen(label)+1)


(void)sprintf(m_pLabel, “%s”, label);




m_LabelLength = -1; // calculate length when needed



size_t GetLabelLength() const {

 if (-1 == m_LabelLength)

m_LabelLength = strlen(m_pLabel); // ERROR!

 return m_LabelLength;




 static int const MAX_LABEL_LEN;

 char *m_pLabel;


int const MyClass::MAX_LABEL_LEN = 1024;

This class may look a bit awkward, but remember that it just serves as an example. Notice how bitwise constness prevents us from caching the length of the label, while conceptually it makes perfect sense. After all, the user won’t notice any changes made to the private data members of this class; its outward appearance (the label) remains exactly the same.

If only we could make an exception for the class itself and allow it to modify its private data members in a const member function.

This is exactly what the keyword ‘mutable’ allows us to do. It can be applied to non-static data members to free them from bitwise constness. It is a very handy keyword that makes your life a bit easier as far as creating const-correct classes are concerned. And as long as your classes are const-correct, others using your classes can be assured that they can maintain const-correctness themselves.

class MyClass { public:




 mutable char *m_pLabel;


If you are still in doubt whether or not your code should be const correct, then please read Item 21 ‘Use const whenever possible’ [Meyers] and try Item 43 ‘Const-Correctness’ [Sutter]. The const keyword was introduced over a quarter of a century ago; make sure you don’t miss out on taking advantage of its value!


[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

- 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