Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
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 |
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.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.