C++ const Correctness

Using const correctly in order to future-proof today's class designs.
About const

Objects declared const have a modified type whose value cannot be changed; they are read-only. For built-in types declared as const, this means that once initialized the compiler refuses to alter their values:


  void inc( int& x ) { x +=1; } 
  const int i = 1;           // initialize constant value
  i += 5;                    // error: 'i' is const
  inc( i );                  // error: 'i' is const

Usually, the const modifier comes immediately before the object it modifies. For example:


  const int ZERO = 0;
  const std::string msg( "Hello, World" );

When applied to pointers, special attention should be paid to the placement of const. A pointer is a variable itself, so the placement of const often determines whether it applies directly to the pointer, to the object pointed to or to both. For example:


  const std::string * str1 = &someString;
  std::string const * str2 = &someString;
  str2 = &anotherString;

Above, str1 is a pointer to a constant string. The const modifies the type std::string. Because there is no const * construct in C++, str2 also is a pointer to a constant string. Both str1 and str2 point to constant strings; therefore, it is impossible to alter the value of the strings they point to. However, it is possible to alter the pointers themselves, and indeed the str2 pointer is changed so that it points to another std::string. Deciphering these declarations can be difficult unless you read them backwards, from right to left. For example, the declaration of str1 can be read from right to left as "str1 is a pointer to a std::string which is const". And str2 can be read as "str2 is a pointer to a constant std::string". Although they are written differently, these declarations describe pointers of a similar type.


  std::string * const str3 = &someString;
  const std::string * const str4 = &someString;

The str3 variable demonstrates a pointer that is constant itself; the pointer value cannot be modified, although the argument pointed to can. It can be read as "str3 is a constant pointer to a std::string". Finally, str4 is the most extreme example. It can be read as "str4 is a constant pointer to a std::string which is const"; neither the pointer nor the string can be modified.

Const Member Functions

The const modifier becomes a part of the object type and also can be applied to user-defined types:


  class Invoice {    
   public:
   void giveDiscount( int d ) {
    //...
   }
  };
  const Invoice i;       // a constant user-defined object
  i.giveDiscount();      // error: 'i' is const

Because Invoice::giveDiscount() has the potential to alter the internal state of the object, the compiler refuses to let you invoke this member function on i, which is declared as const. However, individual member functions themselves can be declared const, which tells the compiler that those member functions do not alter the internal state of the object and therefore are safe to invoke on const objects. For example:


  class Invoice {                    
   public:
    int sum() const { // a constant member function
     //...
    }    
  };
  const Invoice i;    // declares a constant user-defined object
  int s = i.sum();    // ok: Invoice::sum() is const

C++ directly supports const as a language feature, so the compiler provides free compile-time checking of const objects. With properly designed interfaces that utilize const member functions, you are free to declare and use constant user-defined objects--only the const interface of those objects will be available. Designing interfaces that are const correct essentially boils down to declaring const member functions wherever possible. And these const member functions should be included early on in the design of your classes. Trying to retrofit them later often leads to a cascading effect throughout the applications that use your classes. It is much better to use const early and often in your designs.

When designing const member functions, you may encounter a situation in which altering the internal state of the class is necessary. For example:


  class CustomerInvoice : public Invoice {   
   public:
    int sum() const {
     // calculate sum, and save for later
     sumCache = items.size() + custItems.size();  // error
     return sumCache;
    }    
    //...
   protected:
    //...
    int sumCache;
  };

In the above example, the value of sumCache cannot be altered, because CustomerInvoice::sum() is declared to be const. However, we really do want to cache the value for later use; so how can this be done? The answer is "with the mutable keyword", which is the converse of const when applied to member variables. For example, CustomInvoice can be corrected as follows:


  class CustomerInvoice : public Invoice {   
   public:
    int sum() const {
     // calculate sum, and save for later
     sumCache = items.size() + custItems.size();  // ok
     return sumCache;
    }    
    //...
   protected:
    //...
    mutable int sumCache;         // mutable member variable
  };

Now, the compiler allows sumCache to be modified within the CustomerInvoice::sum() const member function because sumCache was declared to be mutable. The mutable keyword instructs the compiler to accept changes to the variable declared with it, even in const member functions.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks

Henning's picture

I just wanted to thank you for this great text, too.
I read lots of tutorials and also a big book about C++ and never got behind the const magic (worse: I misunderstood it!).

This finally made my mind clear about const, references and these things.
Very good work!

Great Article

Argonaut's picture

First, thanks for the article. I found it very informitive and well thought through. So much on the iNet today is certainly less than what we would hope for. (Thanks for putting the time into this)

Second, I appologize for my Rambunctious colleagues. (Assuming they are in the profession of Software Engineering and not just college hackers)

Keep up the great work.

Cheers

Re: C++ const Correctness

Anonymous's picture

Great article! :-)

invalid references

Anonymous's picture

> You never can create an invalid reference,
> so f() does not need to worry about invalid pointers.

How about this :

int* x = NULL;
int& rx = *x;

Isn't rx an invalid reference ?

I saw this many times "there are no invalid references" ... it's simply false.

Re: invalid references

Anonymous's picture

Dereferencing a null pointer yields undefined behaviour, so your example is flawed.

Re: invalid references

Anonymous's picture

Dereferencing a null pointer produces undefined behavior. So you are not so much 'creating an invalid reference' as you are invoking undefined behavior (by dereferencing a null pointer).

Your compiler may accept this program, however at run time it should crash when you dereference 'x'. If it does not, that does not mean you are creating an invalid reference since any behavior after this point is undefined.

Re: invalid references

Anonymous's picture

Correct!

Especially bastardish in multi threaded applications and dynamically created objects.

Sometimes it is really easier to just use pointers because you can at least check if they are != 0...

Re: invalid references

Anonymous's picture

No, since using pointers in function interfaces is ambiguous at best. For example,

void foo( int* x );

Here, there is no indication whether nulls are allowed or not. If they are, who is at fault: the calling function or the called function? Using references removes all ambiguity:

void foo( int& x );

The fewer pointers you have in your interfaces, the less time you will spend running around chasing nulls.

Effective C++

Anonymous's picture

Was just reading Scott Meyer's 2nd ed of Effective C++... It's great to get another walk through on this topic. Articles like this are the best... I don't have to know huge amounts about c++ to learn this one part. Please do more articles like this!!!

Thanks!

Re: C++ const Correctness

Anonymous's picture

That's all fine and well.

But doesn't all this messing around with const really show that C++ is not the greatest language for object-oriented development.Most people are just content to accept this less than adequate language as if there is nothing better.No one to blame but yourselves. Try reading about a number of other.programming language architectures and then, if you are bright enough, you'll realize there are some compelling alternatives available.

By the way, C++ is not a pure object-oriented language.It is a hybrid. True object-oriented languages typically consider all types as objects, not just those types that are instances of classes

And contrary to the author's point of view, the most fundamental unit of development in C++ is not the class. Classes are extensions of structures in C++.

Use a better langauge and these 'const' issues are just noise.

Go figure..

Re: C++ const Correctness

Anonymous's picture

I'm assuming the author is a Java programmer. It always seems like those people that use other languages like java forget where they came from. If you go read those other programming langauges most the time you will find "...uses a C++ like syntax".

Anyways without going into a fifty page rant, it pretty much boils down to this. If you don't like C++ don't use it... unless of course you need to write a driver for your new unsupported sound card.. opps can't do that in java.. sorry.. but you should be ok writting that next high speed 3d shoot em up.. opps.. can't realy do that in java either..

Re: C++ const Correctness

Anonymous's picture

Don't assume. It makes you look less intelligent, especially when you can't spell "oops" correctly. Most people that complain about the speed of Java are simply bad programmers. C and C++ have the brute force to let bad programmers write fast code, where Java will penalize the programmer who really doesn't know how to program, which is generally the case. Just because Java is easy to program, it doesn't mean that it easy to program correctly.

And actually, you can write high speed 3d graphics in Java, just don't try it with Swing or AWT. Unsubstantiated conceits like this betray the meaning of what you were trying to say with your reply..

Despite the author's commments and even Bjarne Stroustrup's various digs at his own language, C++ can be an elegant and effective language, both in it's C and object-oriented forms. Cornering yourself into a single language because of some herd mentality that it is just "better" is just plain silly. Each has it's strengths and weaknesses that should be evaluated for a project, and hopefully the "best" for that project is used.

Re: C++ const Correctness

Anonymous's picture

FORTRAN RULES FOREVER!

Re: C++ const Correctness

Anonymous's picture

True object-oriented languages typically consider all types as objects, not just those types that are instances of classes

Even true object-oriented languages like Java do not treat all types as objects. They have basic data types like int and char. Every language eventually has to be compiled down to machine language and machine language does not recognize objects. Each programming language has its strengths and weaknesses.

True Object Oriented Languages

David Ash's picture

That's true, although the standard Java library does re-implement primitives as classes.

Of course, what takes C++ out of the "pure object-oriented language" category is the fact that it doesn't force all functions to be members of classes, and ultimately uses a non-member function as the program entry point. It has nothing to do with whether the language has primitives (the only languages I've used that *kind of* don't are high-level languages that use scalars -- which are basically primitives.) The original poster simply didn't know what he/she was talking about.

Oh, and by the way, 'const' is a reserved keyword in Java, they just don't use it for anything (yet) to my knowledge. I do know there has been talk to add similar functionality to that language.

Anyways, this guide was exactly what I was looking for when I typed '"c++" +const' into google. Great article!

Re: C++ const Correctness

Anonymous's picture

By the way, C++ is not a pure object-oriented language.It is a hybrid. True object-oriented languages typically consider all types as objects, not just those types that are instances of classes.

Incorrect. Though C++ is a hybrid, and this is a strength because it allows the user to choose the paradigm to employ, an object can be of any type. Objects that are of a class-type are not special in this way. You might only consider arrays not to be first-class objects (but they are objects, just apply sizeof to one), and you can use features like std::vector or boost::array if find that a hindrance.

Re: C++ const Correctness

Anonymous's picture

> And contrary to the author's point of view, the most fundamental unit of development in C++ is not the class. Classes are extensions of structures in C++.

Incorrect. In C++, structures are classes, ones with all members defaulted to having public accessability. This is the only difference between a class and a struct.

Re: C++ const Correctness

Anonymous's picture

Bases and members both.

Re: C++ const Correctness

Anonymous's picture

...as if there is nothing better... if you are bright enough, you'll realize there are some compelling alternatives available.

Thats interesting, if those who could program well while having to deal with all these const are not "bright", then what does it make those who cannot handle it?

Re: C++ const Correctness

Anonymous's picture

An important issue has been left out: overloading of methods on const. This is used extensively in the STL:

class vector {
//...
iterator begin();
const_iterator begin() const;
//...
};

It is very probable that a container class such as vector is implemented with copy-on-write sharing, and that the non-const version of begin() would already cause such a copy (a detach) to happen. I have thus started to maximise the const usage in my applications. I will go to moderate pains to be able to declare a temporary variable const, e.g.:

const std::vector v = someBool ? someFunc() : someOtherFunc() ;

instead of

std::vector v;
if ( someBool )
v = someFunc();
else
v = someOtherFunc();

The pain becomes moderate if you start to nest ternary operators, which can be done surprisingly readable, but there comes always a point where you want to refactor the mess into a function of its own.

Using const temps also forces you to use proper scoping and init-on-declaration, which is a worthwhile goal in itself (it saves ctor and assignment operator calls).

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState