On Tue, 6 Jan 2004 09:24:42 +0530, "S.Senthilvel" <me@somewhere.com>
wrote in comp.lang.c++:
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.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html