473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

const_cast

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.
Jul 22 '05 #1
4 2751
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
Jul 22 '05 #2
S.Senthilvel wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3
> 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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4
Senthilvel Samatharman wrote:
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.
...


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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5

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

Similar topics

3
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
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"; }
18
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
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
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
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
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
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(); }
4
by: George2 | last post by:
Hello everyone, In MSDN sample for const_cast, http://msdn2.microsoft.com/en-us/library/bz6at95h(VS.80).aspx There is a statement like this, --------------------
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
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...

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.