"Kaspar Minosiants" <k-********@yandex.ru> wrote in message
news:bf************@ID-173791.news.uni-berlin.de...
Hi ,
there is a question about work of const_cast
The question is why the object is still reachable after a delete operation
class A {
int var;
public:
A():var(0){}
~A(){}
void setVar(int a){var=a;}
int getVar() const {return var;}
int getVar() {return var;}
};
int main() {
const A* a=new A();
A *b=const_cast<A*>(a);
delete a;
b->setVar(10);
cout<<b->getVar();
return 0;
}
Well what would you expect to happen? Its not a compile error, so what sort
of error?
The truth is that accessing an object after it has been deleted is an
example of 'undefined behaviour', which means ANYTHING could happen,
including your program 'working'.
This has nothing to do with const_cast.
john