13.10 “const”
The const keyword should not be underestimated in C++. When used correctly, it can be very useful to programmers. With it, the compiler can help avoid careless mistakes and discover design problems.
In C++, you can use const in various places: whether passed as a parameter, as a local variable, or as a data field of a class. Here is a (not exhaustive) explanation of what is declared as “immutable” by a const:
-
static const int MAX = 100;Together with static, a constant is defined.
-
const string& getName();The return value must not be changed.
-
void print(const string& msg);The parameter is not changed within the function.
-
void Widget::drawYourself() const;The method does not change its object.
The last item has a special ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access