Item 3: Use const Whenever Possible
You may add const
to almost anything.
- “STL iterators are modeled on pointers.”
-
const iterator
is likeT * const
andconst_iterator
is likeconst T *
. operator*
should return const.
- “Member function differing only in their constness can be overloaded.”
- const member functions: Compiler checks bitwise constness but you usually want logical constness.
-
Logical constness: You may modify the object, but “only in ways that clients cannot detect” (like caching stuff internally).
- You may implement non-const version from const member function using
static_cast
andconst_cast
(but not const-version from non-const member function). This is not pretty but better than duplicating code (especially when the member function is not just one-liner).