The implementation mechanism of Const in C + + a deep analysis of _c language

Source: Internet
Author: User
problem
What does the C language and the const in C + + mean? How is the concrete implementation mechanism realized? This paper will do some analysis on these two problems, simply explain the meaning of const and the implementation mechanism.

Problem Analysis
Simply put, const represents a read-only variable in C and a constant in the C + + language. About the use of Const in C and C + + language and more differences, later have time to open another note.

So how does the const actually achieve it? How does the compiler implement the const intent for built-in types declared as const, such as Int,short,long, and so on? Does the non-built-in type handle the same as the built-in data type, for example, for struct types? Here are a few small examples to illustrate these issues:
C-Language Const example
Copy Code code as follows:

const int i=10;
int *p= (int *) (&i);
*p=20;
printf ("i=%d *p=%d \ n", i,*p);

Guess what the output turns out to be? I=20 *p=20
C + + Language Const Example 1:
Copy Code code as follows:

const int i=10;
int *p=const_cast<int *> (&i);
*p=20;

cout<< "i=" <<i<< "*p=" <<*p<<endl;

The output result is i=10 *p=20
C + + Language Const Example 2:
Copy Code code as follows:

struct test{
Int J;
char tmp;
Test ()
{
j=30;
Tmp= ' a ';
}
};
int main (int argc, char* argv[])
{
const struct Test T1;
int *q= (int *) (&T1.J);
*q=40;
cout<< "j=" <<t1.j<< "*q=" <<*q<<endl;
return 0;
}

The output result is j=40 *q=40

Sample Result Analysis
See the above three sets of output, there is no feeling very strange:
Question 1, for variables in the const int type I,c language, after modifying the value by pointer p, I becomes 20, and in C + +, I is still 10 after modifying the value through the pointer p.
Question 2,c++ The element j of the const struct test in the language is changed by the pointer Q, why is the const int different from the const struct test reaction mechanism?

For question 1, we know that const in C is a read-only variable, since the const is regarded as a variable, it will have the space to store it in memory, and it can indirectly change the value of the memory space through the pointer, when the value of the memory is changed by the pointer P, and then the value of I is retrieved. Accesses the space and gets the changed value. C + + Regards Const as a constant, and the compiler uses constants to replace references to I, such as cout<<i; Will be understood as cout<<10; Does not go to the memory address of I to fetch the data, it is a bit like the C language of the macro #define I 10. So C + + I will output 10, and *p will output 20.

The problem 2,c++ language is only constant substitution for built-in data types, but not for non-built-in data types such as structs. Because the struct type is not a built-in data type, the compiler does not know how to replace it directly, so you have to access the memory to fetch the data, and accessing the memory to fetch the data is bound to take the value of the changed pointer Q, thus creating a completely different processing mode than the const int in C + +

Summary
C language and C + + have a lot of differences and relations, the const is only one aspect of the const understanding and use is the basis for programming, need to master.

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.