const_cast usage.
Definition :It allows the programmer to strip the
Lets start with an example :
const int a = 10;
// ++a; Compiler error as const value can't be changes.
int *ptr = const_cast&a;
*ptr = 80;
cout<<a<<" == "<<*ptr;
OutPut : 10 == 80.
Surprised ?
Value of a is not changed?
Then what is the use of const_cast?
Lets do some analysis : Start with the address of both variables. May be they point to different locations in memory. Thats why the values they contains are different.
if (&a == ptr) {
cout<<"Addresses are same";
}
else {
cout<<"Different addresses";
}
Now the OutPut is :
Addresses are same.
Big surprise : If the addresses are same then how can be value at them are different. How two different values can be reside on the same memory locations.
Here is the reason :
const_cast is remove the constness of the object. This statement is true here too but not visible due to compiler optimization. Actually when we declare any variable as a const then compiler take it as literal. So when ever it looks for "a" in given example it is replaced by 10. That is : After compilation statement look like :
cout<< 10<<" == "<<*ptr;
Even if we use the address operator, the compiler is being smart enough to realize that (*&x) is the same expression as (x), so it's optimizing away the indirection. For Example :
cout<<*(&a)<<" =="<<*ptr;
But the result is same in this case too : 10 == 80.
How can be know that the value at this address is changed or not?
Answer of this question is simple program to try yourself :
const qualifier, making any object modifiable.Lets start with an example :
const int a = 10;
// ++a; Compiler error as const value can't be changes.
int *ptr = const_cast&a;
*ptr = 80;
cout<<a<<" == "<<*ptr;
OutPut : 10 == 80.
Surprised ?
Value of a is not changed?
Then what is the use of const_cast?
Lets do some analysis : Start with the address of both variables. May be they point to different locations in memory. Thats why the values they contains are different.
if (&a == ptr) {
cout<<"Addresses are same";
}
else {
cout<<"Different addresses";
}
Now the OutPut is :
Addresses are same.
Big surprise : If the addresses are same then how can be value at them are different. How two different values can be reside on the same memory locations.
Here is the reason :
const_cast is remove the constness of the object. This statement is true here too but not visible due to compiler optimization. Actually when we declare any variable as a const then compiler take it as literal. So when ever it looks for "a" in given example it is replaced by 10. That is : After compilation statement look like :
cout<< 10<<" == "<<*ptr;
Even if we use the address operator, the compiler is being smart enough to realize that (*&x) is the same expression as (x), so it's optimizing away the indirection. For Example :
cout<<*(&a)<<" =="<<*ptr;
But the result is same in this case too : 10 == 80.
How can be know that the value at this address is changed or not?
Answer of this question is simple program to try yourself :
#include
#include
#include "Main.h"
#include "C.h"
using namespace std;
voidPrintValue(const int*ptr)
{
cout<<*ptr;
}
void main()
{
const inti = 10;
int *k = const_cast<int*>(&i);
*k = 80;
cout<<&i<<" == "<<k<<endl;
cout<<*&i<<" != "<<*k<<endl;
PrintValue(&i);
cout<<" == "<<*k;
_getch();
}
Check the output now.
Thanx

Comments
Post a Comment