Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) const member


The value of a member variable that is declared const cannot be changed after construction.


In class design, a const member reflects an attribute that can be different between objects, but stay constant after construction. When modeling humans (or persons) attributes as name, gender and birth date are candidates for a const member.


struct Person
  Person(const bool isMale) : mIsMale(isMale) {}
  const bool mIsMale;


As a const member cannot be changed after construction, you might choose to make it public, instead of writing a getter.


Use const whenever possible [1-6].


Exercise 5: the many types of const is an exercise about the many types of const.


Const members often make classes noncopyable. A solution to this feature is to store the objects in a boost::shared_ptr and/or using the Pimpl idiom.








  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 7.9.3: 'Use const extensively and consistently'
  2. Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 3: 'Use const whenever possible'
  3. Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Understand and use const in your code'
  4. Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 8, chapter 'Const member functions': 'Use const whenever possible.'
  5. Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 2: 'Prefer consts, enums and inlines to #defines'
  6. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 15: 'Use const proactively'






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict