const_cast

S

S.Senthilvel

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);

// Undefined cast
const int ci = 10;
const int* pci = &ci;

int *pi = const_cast<int*>(pci);

I thought that the result of a const_cast is undefined if the
object pointed to is really a constant.(or is it that modifying that result
is undefined)

In "More Effective C++" , I saw a piece of code which goes this way

class SpecialWidget;
void update(SpecialWidget* psw);

const SpecialWidget sw;
update(const_cast<SpecialWidget*>(&sw));

Here Meyers says that sw can now be safely updated inside "update" function.
I fell this is contradictory to what i have understood.
Can anyone kindly explain.

Thanks,
Senthilvel.
 
J

Jack Klein

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);

// Undefined cast
const int ci = 10;
const int* pci = &ci;

int *pi = const_cast<int*>(pci);

I thought that the result of a const_cast is undefined if the
object pointed to is really a constant.(or is it that modifying that result
is undefined)

No, the cast is fine. It is only modifying any object that was
actually defined as const that is undefined.
In "More Effective C++" , I saw a piece of code which goes this way

class SpecialWidget;
void update(SpecialWidget* psw);

const SpecialWidget sw;
update(const_cast<SpecialWidget*>(&sw));

Here Meyers says that sw can now be safely updated inside "update" function.
I fell this is contradictory to what i have understood.
Can anyone kindly explain.

Thanks,
Senthilvel.

If Meyers wrote this, he is just plain wrong. On some implementations
with memory-management hardware, the processor may well generate a
hardware trap and terminate the program. On an embedded system, const
objects may be stored with the code in ROM or Flash memory devices
that are literally not writable by normal programs, or at all, and
there will be no trap but the value will remain unchanged. As far as
the C++ standard is concerned, anything can happen if you try to
modify an object actually defined as const.
 
A

Andrey Tarasevich

S.Senthilvel said:
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);

// Undefined cast
const int ci = 10;
const int* pci = &ci;

int *pi = const_cast<int*>(pci);

I thought that the result of a const_cast is undefined if the
object pointed to is really a constant.(or is it that modifying that result
is undefined)

The result of 'const_cast' is defined. But an attempt to modify the
constant object through the resultant pointer will result in undefined
behavior.
In "More Effective C++" , I saw a piece of code which goes this way

class SpecialWidget;
void update(SpecialWidget* psw);

const SpecialWidget sw;
update(const_cast<SpecialWidget*>(&sw));

Here Meyers says that sw can now be safely updated inside "update" function.
I fell this is contradictory to what i have understood.
Can anyone kindly explain.
...

I took a look into my copy of "More Effective C++" and the closest thing
I could find looked as follows (code from Item 13, modified for brevity):

SpecialWidget sw;
const SpecialWidget& csw = sw;
update(const_cast<SpecialWidget*>(&csw));

Note that this is not the same as your code. This code does not attempt
to modify a constant object.
 
S

Senthilvel Samatharman

I took a look into my copy of "More Effective C++" and the closest thing
I could find looked as follows (code from Item 13, modified for brevity):

SpecialWidget sw;
const SpecialWidget& csw = sw;
update(const_cast<SpecialWidget*>(&csw));

Note that this is not the same as your code. This code does not attempt
to modify a constant object.
Thanks for your answer Andery..

But I do have the piece of code i've mentioned in "Item 2 : Prefer C++ style
casts"

on page 13 of my "More Effective C++".

It may be that i have a old edition of it..I'm having a Fourth Indian
Reprint, 2001.



Best Regards,

Senthilvel.

 
A

Andrey Tarasevich

Senthilvel said:
Thanks for your answer Andery..

But I do have the piece of code i've mentioned in "Item 2 : Prefer C++ style
casts"

on page 13 of my "More Effective C++".

It may be that i have a old edition of it..I'm having a Fourth Indian
Reprint, 2001.
...

Oh, my mistake. I was also referring to Item 2 in "More Effective C++"
(I don't know where I got that "Item 13" from). I'm using CD version of
the book, which is based, AFAIK, on the 1996 edition. It is strange that
your edition is so different.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top