Language definition of const
I was surprised to find this "hole" in "const"ness:
There is none.
const
applies uniformly to all class members: in a const
member function of class C
, this
has type const C *
, so for a member C::mem
declared as type T
:
class C {
// ...
T mem;
};
this->mem
has type const T
.
Please take type to discern what is the declared type T
and the corresponding const-qualified type for all the members.
Seems like a problem (it kind of was in my code, where I found it.)
Just because the systematic application of rules does not do what you expected does not mean there is a problem with the rules, it means there is a problem with your expectations.
You should write down your expectations to see that you expected a non uniform application if const
to different types.
When you program, you have to reason logically. You should infer things, not expect them when there is no logical reason.
Using const
correctly
Why is that?
Your classes being called A
and B
, it's pretty hard to understand what constitute logical state and what does not. ;) You ask a "moral" question (not a question only on legal/illegal C++ programs), and your code fragment has no "moral" value. If you actually post relevant code, we might make some "moral" judgements about it.
Logical state
You should declare const
the functions that do not change the "logical state" of the object it's applied to.
It means you have to define what is the "logical state" of your class instances: it's an abstract concept, and only you can define it, because it's a high level concept. The "logical state" relates to the problem your class should solve.
Then you can discern which variables contribute to the logical state: does *(b.aPtr)
contribute to the logical state of b
?
Closely related questions
Do you know about the copy constructor?
About the copy assignment operator?
About the destructor?