Transferred from: http://blog.csdn.net/lwbeyond/article/details/6213382
A. Function Description:
Const_cast < Type-id > (expression)
The main use is to remove the const attribute, but you can also add the const attribute. Mainly use the former, the latter is seldom used.
Remove the const attribute:const_case<int*> (&num), often, because a const variable cannot be assigned directly to a non-const variable and must be converted.
Add the Const attribute: const int* k = Const_case<const int*> (j), which is rarely used because a non-const variable can be assigned directly to a const variable, such as: const int* k = j;
Two. Scope of use:
1. The constant pointer is converted to a non-const pointer, and the converted pointer points to the original variable (that is, the converted pointer address does not change).
[CPP]View Plaincopy
- Class A
- {
- Public
- A ()
- {
- M_inum = 0;
- }
- Public
- int m_inum;
- };
- void Foo ()
- {
- //1. Pointer to class
- const A *PCA1 = new A;
- A *PA2 = const_cast<a*> (PCA1); //Constant object converted to a very mass object
- Pa2->m_inum = 200; //fine
- //The pointer points to the original object after conversion
- cout<< Pca1->m_inum <<pa2->m_iNum<<endl; //200
- //2. Pointers to basic types
- const int ica = 100;
- int * ia = const_cast<int *> (&ica);
- *ia = 200;
- cout<< *ia <<ica<<endl; //200
- }
2. A constant reference is converted to a very good reference.
[CPP]View Plaincopy
- Class A
- {
- Public
- A ()
- {
- M_inum = 1;
- }
- Public
- int m_inum;
- };
- void Foo ()
- {
- A A0;
- Const A &A1 = a0;
- A A2 = const_cast<a&> (A1); //constant reference to a very reference
- A2.m_inum = 200; //fine
- cout<< a0.m_inum << a1.m_inum << a2.m_inum << Endl; //1 1
- }
2. A constant object (or base type) cannot be converted to a non-const object (or base type).
[C-sharp] view Plaincopy
- void Foo ()
- {
- An error occurred when//constant object was converted to a very mass object
- Const A CA;
- A = const_cast<a> (CA); //Not allowed
- const int i = 100;
- int j = const_cast<int> (i); //Not allowed
- }
Remember that this conversion only opens an interface, not a substantial conversion. (In fact, it is actually converted, but the expression is not allowed to write this)
3. Add a const attribute
[C-sharp] view Plaincopy
- int main (int argc, char * * argv_)
- {
- int i = 100;
- int *j = &i;
- const int *k = const_cast<const int*> (j);
- //const int *m = j; It's almost like it's written .
- //refers to the same address
- cout <<i<<"," <<&i<<endl; //100, 0012ff78
- cout <<*j<<"," <<j<<endl; //100, 0012ff78
- cout <<*k<<"," <<k<<endl; //100, 0012ff78
- *j = 200;
- //*k = 200; Error
- return 0;
- }
Three. Summary:
1. Using Const_cast to remove the const attribute does not really change the const property of the original class type (or base type), it simply provides an interface (pointer or reference) that allows you to change the value of the type through this interface. Maybe that's one reason const_case can only convert pointers or references.
2. Adding a const attribute using Const_case also provides an interface to not modify its value, but this added const operation has no practical purpose (perhaps I know too shallow).
C + + type conversion detailed--const_cast (reprint)