473,486 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

MSDN const_cast sample

200 New Member
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,

--------------------
On the line containing the const_cast, the data type of the this pointer is const CCTest *. The const_cast operator changes the data type of the this pointer to CCTest *
--------------------

I think in a const member function, like void printNumber() const, the type of this pointer is const pointer to current type, so we use this const_cast to remove its const properties.

For a non-const member function, I think this pointer should not be a const pointer, right?

So, conclusion is,

1. in const member function, this pointer is a const pointer;
2. in non-const member function, this pointer is a non-const pointer.

Right?


thanks in advance,
George
Dec 17 '07 #1
4 1808
weaknessforcats
9,208 Recognized Expert Moderator Expert
Most of your post is so not true.

Let's take it in pieces.

First, the const member function. This is a function that cannot change the member data. Not being able to chnage the member data makes the this pointer const. I believe that ius what you concluded and you are correct.

Second, a const member function can change members of a const object if those member are marked mutable. The mutable qualifer means that the member does not participate in the const-ness of the object.

Third, in a non-const member function, the this pointer is not const and I believe this was also one of your conclusions. However, even though the this pointer is not const, you still cannot change the vaue of any const members.

Fourth, the const_cast does not remove the const. What it does is create a copy that is not const. You are free to change the copy. However, the original is untouched. Worse, the address of the original and the address of the non-const copy are reported as the same. Check this out:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    const int var = 1234;
  4.    //int* ptr = &var;    //error! var is const
  5.    int* ptr = const_cast<int*> (&var);
  6.   *ptr = 9999;
  7.    cout << "var using ptr = " << *ptr << endl;
  8.    cout << "var using var = " << var << endl;
  9.    cout << "&var using ptr = " << ptr << endl;
  10.    cout << "&var using var = " << &var << endl;
  11. }
  12.  
This is vile. Now the program uses 1234 when var is used and 9999 when *ptr is used. Clearly, there are now two variables.

You see, var is const and the compiler never permits the value of a constant to change.

According to Stroustroup, using a const_cast is to change a variable is not guaranteed to work if the object was declared as const. That is, if the variable were originally non-const and was later to be accesses using a const pointer, you could const_cast the the const pointer to non-const and change the original object.

That is, a const_cast allows pointer acess to a variable but the result of using that pointer to change the value of the variable is undefined.

You see the same effect using the C-style cast that is depreacted in C++:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    const int var = 1234;
  4.    //int* ptr = &var;    //error! var is const
  5.    int* ptr = (int*)&var;
  6.   *ptr = 9999;
  7.    cout << "var using ptr = " << *ptr << endl;
  8.    cout << "var using var = " << var << endl;
  9.    cout << "&var using ptr = " << ptr << endl;
  10.    cout << "&var using var = " << &var << endl;
  11. }
  12.  
Dec 17 '07 #2
George2
200 New Member
Thanks weaknessforcats,


Only one comment about the following code,

Expand|Select|Wrap|Line Numbers
  1.    const int var = 1234;
  2.    //int* ptr = &var;    //error! var is const
  3.    int* ptr = const_cast<int*> (&var);
  4.  
I think &var is const int* type, so you need to use const_cast remove const qualifier and makes &var int* type, right?

Most of your post is so not true.

Let's take it in pieces.

First, the const member function. This is a function that cannot change the member data. Not being able to chnage the member data makes the this pointer const. I believe that ius what you concluded and you are correct.

Second, a const member function can change members of a const object if those member are marked mutable. The mutable qualifer means that the member does not participate in the const-ness of the object.

Third, in a non-const member function, the this pointer is not const and I believe this was also one of your conclusions. However, even though the this pointer is not const, you still cannot change the vaue of any const members.

Fourth, the const_cast does not remove the const. What it does is create a copy that is not const. You are free to change the copy. However, the original is untouched. Worse, the address of the original and the address of the non-const copy are reported as the same. Check this out:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    const int var = 1234;
  4.    //int* ptr = &var;    //error! var is const
  5.    int* ptr = const_cast<int*> (&var);
  6.   *ptr = 9999;
  7.    cout << "var using ptr = " << *ptr << endl;
  8.    cout << "var using var = " << var << endl;
  9.    cout << "&var using ptr = " << ptr << endl;
  10.    cout << "&var using var = " << &var << endl;
  11. }
  12.  
This is vile. Now the program uses 1234 when var is used and 9999 when *ptr is used. Clearly, there are now two variables.

You see, var is const and the compiler never permits the value of a constant to change.

According to Stroustroup, using a const_cast is to change a variable is not guaranteed to work if the object was declared as const. That is, if the variable were originally non-const and was later to be accesses using a const pointer, you could const_cast the the const pointer to non-const and change the original object.

That is, a const_cast allows pointer acess to a variable but the result of using that pointer to change the value of the variable is undefined.

You see the same effect using the C-style cast that is depreacted in C++:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    const int var = 1234;
  4.    //int* ptr = &var;    //error! var is const
  5.    int* ptr = (int*)&var;
  6.   *ptr = 9999;
  7.    cout << "var using ptr = " << *ptr << endl;
  8.    cout << "var using var = " << var << endl;
  9.    cout << "&var using ptr = " << ptr << endl;
  10.    cout << "&var using var = " << &var << endl;
  11. }
  12.  

regards,
George
Dec 19 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
According to Stroustroup, using a const_cast is to change a variable is not guaranteed to work if the object was declared as const. That is, if the variable were originally non-const and was later to be accesses using a const pointer, you could const_cast the the const pointer to non-const and change the original object.

That is, a const_cast allows pointer acess to a variable but the result of using that pointer to change the value of the variable is undefined.
Did you not read this in my previous post???

Using a const_cast on a const object produces undefined results. That is, you can't say what will happen.

The whole point of a const_cast is when you have a const pointer to a non-const object and you wish to change the object. Here a const_cast can create a non-const pointer to the non-const object so you can change it.

I say again, casting in C++ will nearly always put you in a situation where you prpgram can crash. Casting in C++ usually means a) you care calling a relic C function that has a void* argument, or b) your C++ design is screwed up.
Dec 19 '07 #4
George2
200 New Member
Thanks for your advice, weaknessforcats!


I agree and my question is answered.

Did you not read this in my previous post???

Using a const_cast on a const object produces undefined results. That is, you can't say what will happen.

The whole point of a const_cast is when you have a const pointer to a non-const object and you wish to change the object. Here a const_cast can create a non-const pointer to the non-const object so you can change it.

I say again, casting in C++ will nearly always put you in a situation where you prpgram can crash. Casting in C++ usually means a) you care calling a relic C function that has a void* argument, or b) your C++ design is screwed up.

regards,
George
Dec 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5376
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
4290
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"; }
4
2763
by: S.Senthilvel | last post by:
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);
18
4815
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
1614
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
2042
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
2133
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
1915
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...
2
2126
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
7094
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
6964
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
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.