473,320 Members | 1,699 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,320 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 2731
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.