Re: Const member function
On Jun 15, 2:44 pm, Wolfgang <techreposit...@gmail.com> wrote:
As I understand, a const member function is used by const object to
ensure that its instance isn't modified throughout its life. Am I
missing something..
Others have pointed out the technical aspects: the compiler only
understands what is called "bitwise" const. If you think about
it, how can it be otherwise? How can the compiler know whether
a pointer is part of the implementation of the object, and what
it points to is logically part of the object, or whether it is
there to allow navigation to another object, and of course, the
const-ness of the object which contains the pointer doesn't
affect the const-ness of the other object.
What hasn't been mentionned is the fact that you, as author of
the class, do know which pointers are part of your
implementation, and which ones are just for navigation. In a
very real sense, you define what const means for your object.
If parts of the basic bits are not part of your object's "value"
(as seen by client code), you can cast away const or use mutable
to modify them even in const functions. (The classical example
is a cached value, but I find that reference counters and mutexs
tend to occur more frequently.) And if something "pointed to"
is logically part of you object, you don't modify it from a
const function, even though the compiler allows it. This is
called "logical const", and I think that there is pretty much a
consensus today that this is how const in C++ should be used.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34