[LC++]How to use const member functions correctly?
mark at austrics.com.au
Fri Sep 21 20:58:52 UTC 2001
Things have been a bit quiet on this list of late. Time
to ask a question!
My understanding of const member functions, is that by declaring
it constant, you are giving a guarentee that it will not change
the internals of the object.
But I have a problem. What if I have a member function which has
a parameter passed by reference (non-constant). Passing by
reference allows the function to modify an external variable.
Normally this external variable will be unrelated to the internals
of the object, so for most of the time, this function may be thought
of as const. But what if somewhere, this function is used to modify
an internal variable of the object --- by passing it in as the
reference parameter --- then all of a sudden the function is non-const!
What should be done in this situation? Should I declare it const, and
rely on the programmer to take care not to pass in the wrong parameter?
If I don't make it const, then I can't call the function from other
const member functions, even though I know that in this case it is
not changing internals.
What to do??
More information about the tuxCPProgramming