What is the use of const when the member variables are changed by the
function?
As @songyuanyao correctly mentioned, to cause compile errors.
Yet, it is rather a convention. You may still modify members via const_cast
on this
, or via marking members mutable
.
There's a difference between logical and physical constness, as discussed here.
Why may we still modify non-const
static
members in const
methods?
A non-static
method of a class has this
as a parameter. const
qualifier on a method makes this
constant (and triggers a compile error when the convention's violated).
A static
member isn't related to this
in any way: it is the only one for every object of a class. That's why the constness of a method (i.e. the constness of this
) has no impact on static
members of a class.