C++ methods allow a const qualifier to indicate that the object is not changed by the member function. But what does that mean? Eg. if the instance variables are pointers, does it mean that the pointers are not changed, or also that the memory to which they point is not changed?
Concretely, here is a minimal example class
class myclass {
int * data;
myclass() {
data = new int[10];
}
~myclass() {
delete [] data;
}
void set(const int index) const {
data[index] = 1;
}
};
Does the method set correctly qualify as const? It does not change the member variable data, but it sure does change the content of the array.
Most succinctly, it means that the type of
thisisconst T *inside const member functions, whereTis your class, while in unqualified functions it isT *.Your method
setdoes not changedata, so it can be qualified as const. In other words,myclass::datais accessed asthis->dataand is of typeint * const.