C + + type conversion detailed--const_cast (reprint)

Source: Internet
Author: User

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
  1. Class A
  2. {
  3. Public
  4. A ()
  5. {
  6. M_inum = 0;
  7. }
  8. Public
  9. int m_inum;
  10. };
  11. void Foo ()
  12. {
  13. //1. Pointer to class
  14. const A *PCA1 = new A;
  15. A *PA2 = const_cast<a*> (PCA1); //Constant object converted to a very mass object
  16. Pa2->m_inum = 200; //fine
  17. //The pointer points to the original object after conversion
  18. cout<< Pca1->m_inum <<pa2->m_iNum<<endl; //200
  19. //2. Pointers to basic types
  20. const int ica = 100;
  21. int * ia = const_cast<int *> (&ica);
  22. *ia = 200;
  23. cout<< *ia <<ica<<endl; //200
  24. }

2. A constant reference is converted to a very good reference.

[CPP]View Plaincopy
  1. Class A
  2. {
  3. Public
  4. A ()
  5. {
  6. M_inum = 1;
  7. }
  8. Public
  9.   int m_inum;
  10. };
  11. void Foo ()
  12. {
  13. A A0;
  14.   Const A &A1 = a0;
  15. A A2 = const_cast<a&> (A1); //constant reference to a very reference
  16. A2.m_inum = 200; //fine
  17. cout<< a0.m_inum << a1.m_inum << a2.m_inum << Endl; //1 1
  18. }

2. A constant object (or base type) cannot be converted to a non-const object (or base type).

[C-sharp] view Plaincopy
  1. void Foo ()
  2. {
  3. An error occurred when//constant object was converted to a very mass object
  4. Const A CA;
  5. A = const_cast<a> (CA); //Not allowed
  6. const int i = 100;
  7. int j = const_cast<int> (i); //Not allowed
  8. }

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
  1. int main (int argc, char * * argv_)
  2. {
  3. int i = 100;
  4. int *j = &i;
  5. const int *k = const_cast<const int*> (j);
  6. //const int *m = j; It's almost like it's written .
  7. //refers to the same address
  8. cout <<i<<"," <<&i<<endl; //100, 0012ff78
  9. cout <<*j<<"," <<j<<endl; //100, 0012ff78
  10. cout <<*k<<"," <<k<<endl; //100, 0012ff78
  11. *j = 200;
  12. //*k = 200; Error
  13. return 0;
  14. }


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.