Const and const_cast of C ++

Source: Internet
Author: User

Const and const_cast of C ++

 

Today, I visited the Q & A community,

In C ++, someone asked about const_cast,

Exactly in < >.

I flipped through the books and searched the internet,

I found it quite interesting ....

 

The subject asks how to remove a const array of the double type set to const during running and then change the array content.

 

> First, make it clear that,

For const_cast

This is just a const for pointers and references. For variables, problems may occur.

For example, read the following code:

 

Const int a = 789; int & B = const_cast
 
  
(A); int * c = const_cast
  
   
(& A); cout <run:
   

 

 

Nice to play ~.~

 

 

 

> Then, for this question,

The array belongs to the pointer category because it is an array,

I tried to write,

Found,

Through an intermediate variable, you can change the content of the original const:

 

Const double arr [3] = {1.2, 3.3, 4.5}; int I; for (I = 0; I <3; ++ I) cout <(arr [0]); for (I = 0; I <3; ++ I) cout <The result is OK.

 

 

OK, that's it,

Interesting stuff ~.~

 

 

 

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.