Some little-known things of const

Source: Internet
Author: User
Some of the little-known things of const-Linux general technology-Linux technology and application information. The following is a detailed description. # Include
Using namespace std;

Int main ()
{
Const int k = 60000; // The const int type k cannot be changed

Int * lp; // declare an int-type pointer variable

Lp = (int *) & k; // force convert lp to k

* Lp = 50000; // assign values. Generally, assign values to k.

Cout < Cout <* lp < Cout <& k < Cout <


Return 0;
}



Running result:
0x22ff3c
50000
0x22ff3c
60000

The lp address and k address are the same.
However, the values are different. It is very strange to pass other tests. * lp is not lost. Just like the k value of a common variable, the original value remains unchanged.
By discussing with others, we all think that the compiler is a problem (both eclipse and vc ++ can be compiled)
I searched for half-day data yesterday and still thought that const could only be half-solution (to cope with general situations)

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.