Possible Duplicate:
Why isn't the const qualifier working on pointer members on const objects?
Consider the following class that has a pointer member int *a. The const method constMod is allowed by the compiler even though it modifies the pointer data. Why doesn’t the compiler make the pointer data const in the context of the const method? If a was just an int we wouldn’t be allowed to modify it in a const method.
class ConstTest
{
public:
ConstTest(int *p): a(p) {}
void constMod() const {
++(*a);
}
int *a;
};
I’m using g++ on linux.
It’s just an ownership issue… there’s no way the compiler can know if the pointed-to object is logically part of the object or not, so it’s left for the programmer to police such issues.
constmembers are allowed to perform operations with side effects, as long as they don’t modify their own apparent value. It’s not really any different from letting them call saystd::cout::operator<<()or some other non-const function….