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
4 1808
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: -
int main()
-
{
-
const int var = 1234;
-
//int* ptr = &var; //error! var is const
-
int* ptr = const_cast<int*> (&var);
-
*ptr = 9999;
-
cout << "var using ptr = " << *ptr << endl;
-
cout << "var using var = " << var << endl;
-
cout << "&var using ptr = " << ptr << endl;
-
cout << "&var using var = " << &var << endl;
-
}
-
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++: -
int main()
-
{
-
const int var = 1234;
-
//int* ptr = &var; //error! var is const
-
int* ptr = (int*)&var;
-
*ptr = 9999;
-
cout << "var using ptr = " << *ptr << endl;
-
cout << "var using var = " << var << endl;
-
cout << "&var using ptr = " << ptr << endl;
-
cout << "&var using var = " << &var << endl;
-
}
-
Thanks weaknessforcats,
Only one comment about the following code, -
const int var = 1234;
-
//int* ptr = &var; //error! var is const
-
int* ptr = const_cast<int*> (&var);
-
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: -
int main()
-
{
-
const int var = 1234;
-
//int* ptr = &var; //error! var is const
-
int* ptr = const_cast<int*> (&var);
-
*ptr = 9999;
-
cout << "var using ptr = " << *ptr << endl;
-
cout << "var using var = " << var << endl;
-
cout << "&var using ptr = " << ptr << endl;
-
cout << "&var using var = " << &var << endl;
-
}
-
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++: -
int main()
-
{
-
const int var = 1234;
-
//int* ptr = &var; //error! var is const
-
int* ptr = (int*)&var;
-
*ptr = 9999;
-
cout << "var using ptr = " << *ptr << endl;
-
cout << "var using var = " << var << endl;
-
cout << "&var using ptr = " << ptr << endl;
-
cout << "&var using var = " << &var << endl;
-
}
-
regards,
George
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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";
}
|
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);
|
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...
|
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 {
| |
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...
|
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)
|
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...
|
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,
--------------------
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |