
Originally Posted by
kempofighter
I disagree with your assertion completely. A const member function cannot change the state of objects that it has a pointer to even if the pointer is non-const (at least not without some hacking).
I wish you were right but the code below compiles without errors or warnings.
Code:
class NotConstObject {
private:
int i;
public:
void nonConstMember(){
i=0;
}
};
class ConstObject{
private:
NotConstObject *nco;
public:
ConstObject(NotConstObject *nco): nco(nco){
}
void operator()() const {
nco->nonConstMember();
}
};
main(void){
NotConstObject nco;
const ConstObject o(&nco);
o();
return 0;
}
The reason why your code fails is because of the following line where a class attribute is changed.
Code:
theCollector.insert(rhs[last]);

Originally Posted by
kempofighter
A const function can call non-const functions on input arguments if the input arguments aren't const. If a const function receives a non-const array as an input it can modify the input array but cannot modify the state of the object that it belongs to. Understand?
Yes i do but that's not my problem. My problem is simply that
Code:
nco->nonConstMember();
does not cause any compiler errors.
My compiler is "g++ (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]". Maybe your compiler will refuse to compile the above code in which case it's an interpretation difference issue. If so, which compiler complies with which C++ standard? And which one makes more sense?