Home arrow C++ arrow Page 5 - 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 Member Functions
(Page 5 of 6 )

Another use of const not available in C, but very useful in C++, is the possibility to declare member functions const. This means that you are promising that the member function won’t change the value of any of the data members of the object.

Imagine what would happen when the function membersize() of some class List would change the order of elements in that list while you were iterating through it! By declaring the memberconst it is guaranteed that this cannot happen, and it will force any other programmer that will be maintaining that code not to touch the member values either.

The declaration of a constant member functionsize() looks like this:

size_t size() const; 

You will find them most often used when a class doesn’t expose its data members in the public interface [Meyers]. Such a class will provide ‘get’ and ‘set’ functions in its public interface, and since it makes sense for something that reads out a value not to change that value (or any other values in the object that is being accessed), you will notice ‘get’ functions declared as constant member functions.

A const member function guarantees that an object’s data members will remain untouched, so these functions can only be invoked on a const object. Member functions can behave differently depending on whether they are declared const or not. They can be overloaded exactly for this purpose. That is why you will find two implementations of ‘operator[ ]’ in the STL std::vector class:

// returns a const reference to an element you can only read

const_reference operator[](size_type _Pos) const

{ // subscript nonmutable sequence

return (*(begin()+_Pos));


// returns a reference to an element you can manipulate

reference  operator[](size_type _Pos)

{ // subscript mutable sequence

 return (*(begin()+_Pos));


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