I was surprised to find this "hole" in "const"ness:
#include <stdio.h>
class A
{
int r ;
public:
A():r(0){}
void nonconst()
{
puts( "I am in ur nonconst method" ) ;
r++;
}
} ;
class B
{
A a ;
A* aPtr ;
public:
B(){ aPtr = new A() ; }
void go() const
{
//a.nonconst() ; // illegal
aPtr->nonconst() ; //legal
}
} ;
int main()
{
B b ;
b.go() ;
}
So basically from const
method B::go()
, you can invoke the non-const member function (aptly named nonconst()
) if object of type A
is referenced by a pointer.
Why is that? Seems like a problem (it kind of was in my code, where I found it.)
See Question&Answers more detail:os