"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
avier wrote: hi stu,
thanks for the prompt help.
Does it mean that the data in the object will not be modified?
It does not.
const for member function is just a hint to the reader that the
function does not change logical state of an object, although object's
physical state (the bits it's comprised of) may well change.
It is far more than a hint.
1. It is a promise that the function won't modify the object's data unless
that data is declared mutable --- a promise that the compiler will enforce.
2. It is a required promise for any function that is called on a const
object --- a requirement that, once again, the compiler will enforce.
For 1., observe that the following won't compile because Set changes the
object in spite of promising not to:
class Test
{
int x;
public:
Test() : x(0)
{}
void Set(int x_) const
{
x = x_; // won't compile; violates the const promise
}
};
For 2., observe that the following won't compile because of the absence of
the const qualifier on the DoNothing function, i.e., the fact that the
DoNothing function does nothing is insufficient; it must declare this fact
with the const keyword:
class Test
{
public:
Test()
{}
void DoNothing()
{}
};
int main()
{
const Test t;
t.DoNothing(); //won't compile; t is declared const but DoNothing isn't
return 0;
}
--
John Carson