473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[help] const_cast


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;
}
Jul 19 '05 #1
2 2527
> 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;
}


Undefined behaviour, anything might happen. const_cast has nothing to do
with this.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #2

"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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5378
by: drowned | last post by:
all right, check it out... I've got a practice exercise from "thinking in c++" whose answer isn't covered in the annotated solutions guide, so I'm trying to handle it, but I don't understand what...
7
4293
by: R. Anbeeswaran | last post by:
Hi All, void main() { const int i = 20; int *p = const_cast<int*>(&i); *p = 40; cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n"; }
4
2769
by: S.Senthilvel | last post by:
hi, I am a little confused about the const_cast.I've thought it this way till now. //Proper cast int i = 10; const int* pci = &i; int *pi = const_cast<int*>(pci);
18
4819
by: johny smith | last post by:
Can someone please give me some good reference site/information for understanding when and why I would use 1.) const_cast Don't know why you would do this? 2.) reinterpret_cast. I have used...
6
1617
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
20
2046
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
6
2135
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
6
1919
by: Alexander Stippler | last post by:
Hello, I wonder if and in what cases casting const away by a const_cast can be dangerous. In one place the only way I can imagine to realize a pretty nice feature is casting away const. Now I...
11
5007
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.