Effective C++ item 3: Use const whenever possible
1 | class Rational |
If you miss the first const
, one simple typo can make it wrong:
1 | Rational a, b, c; |
In the code above, I know you want to type ==
not =
. But compiler will happily generate code for you, and maybe you have to spend hours to debug. All you need to do is use const to prevent unwanted behavior.
Let’s see an example:
1 | class TextBlock |
And then to illustrate how const member function works:
1 | TextBlock nonConstObj("Hello"); |
Typically corresponding const and non-const member functions are very similar, to avoid code duplication, we make non-const member function call const member function with certain cast:
1 | class TextBlock |
As you can see, the code has two casts, not one. We want the non-const operator[]
to call the const one, but if, inside the non-const operator[]
, we just call operator[]
, we’ll recursively call ourselves. So we cast *this
from TextBlock&
to const TextBlock&
. The static_cast
is to remove the const from the const operator[]
‘s return value.
For example:
1 | class TextBlock |
Here, m_textLength
can be changed in const member function.