Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const_cast

Reply
Thread Tools

const_cast

 
 
Podrzut_z_Laweczki
Guest
Posts: n/a
 
      07-31-2006
Hello everyone, I have a question:

const double zaq = 1.23;
const double *ptcd = &zaq;
double *ptd;
ptd = const_cast<double*>(ptcd);
*ptd = 5.67;
cout<<zaq<<endl;

It's only an exercise. I tried that in VS.NET 2003 and it seems to work
only with floating point types. I know it's not the best way, but I'm
just curious why this example doesn't work for other types.

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      07-31-2006

"Podrzut_z_Laweczki" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> Hello everyone, I have a question:
>
> const double zaq = 1.23;
> const double *ptcd = &zaq;
> double *ptd;
> ptd = const_cast<double*>(ptcd);
> *ptd = 5.67;
> cout<<zaq<<endl;
>
> It's only an exercise. I tried that in VS.NET 2003 and it seems to work
> only with floating point types. I know it's not the best way, but I'm
> just curious why this example doesn't work for other types.
>


In what way does it "work"? It's not that this "isn't the best way", but
that it's just plain wrong. Using a trick to allow it to happen doesn't
make it legal. It's not. It is Undefined Behavior to change something
which you've declared as const. (Undefined Behavior means it may work, or
it may not; it may crash, or it may not; it _may_ do just about anything.)

-Howard



 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      07-31-2006

Podrzut_z_Laweczki wrote:
> Hello everyone, I have a question:
>
> const double zaq = 1.23;
> const double *ptcd = &zaq;
> double *ptd;
> ptd = const_cast<double*>(ptcd);
> *ptd = 5.67;
> cout<<zaq<<endl;
>
> It's only an exercise. I tried that in VS.NET 2003 and it seems to work
> only with floating point types. I know it's not the best way, but I'm
> just curious why this example doesn't work for other types.


Modification of a const object is always undefined. The behavior of
such undefined behavior is thus often times rather unpredictable and
undefined.

 
Reply With Quote
 
Podrzut_z_Laweczki
Guest
Posts: n/a
 
      07-31-2006
Howard wrote:
> In what way does it "work"? It's not that this "isn't the best way", but
> that it's just plain wrong. Using a trick to allow it to happen doesn't
> make it legal. It's not. It is Undefined Behavior to change something
> which you've declared as const. (Undefined Behavior means it may work, or
> it may not; it may crash, or it may not; it _may_ do just about anything.)


I didn't intend to use that, but I get the picture now. Thank you.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const_cast question Gajanan Bhat C++ 9 05-05-2004 12:29 PM
const_cast S.Senthilvel C++ 4 01-08-2004 06:59 PM
const_cast<> R. Anbeeswaran C++ 7 11-14-2003 12:15 PM
const_cast question drowned C++ 3 08-04-2003 08:51 AM
[help] const_cast Kaspar Minosiants C++ 2 07-21-2003 12:43 PM



Advertisments