Discuss the const implementation mechanism in C ++

Source: Internet
Author: User
Q: What exactly does const in C language and C ++ indicate? How is the specific implementation mechanism implemented? This article will analyze the two issues and briefly explain the meaning and Implementation Mechanism of Const.
Problem Analysis
To put it simply, const represents a read-only variable in C language and a constant in C ++ language. For more differences between the use of const in C and C ++, I will post a new note later.
So how is const implemented? For built-in types declared as const, such as int, short, and long, how does the compiler implement the const intention? Are non-built-in data types processed the same way as built-in data types? For example, what will happen to struct types? Below are several small examples to illustrate these problems:
C const example:
const int i=10;int *p=(int *)(&i);*p=20;printf("i=%d   *p=%d   \n",i,*p);

Guess what the output result is? I = 20 * P = 20
C ++ const Example 1:

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 ++ const Example 2:

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
Are you surprised to see the above three sets of output results: Problem 1: For the const int type variable I, after the C language modifies the value through the pointer P, I becomes 20; in C ++, after the value is modified through the pointer P, the I is still 10. Question 2: The element J of const struct test in C ++ is changed through the pointer Q. Why is the reaction mechanism of const int different from that of const struct test?
To address Issue 1, we know that const in C indicates a read-only variable. Since const is regarded as a variable, it will have storage space in the memory, in addition, the value of the memory space can be indirectly changed through the pointer. When the value in the memory is changed through the pointer P and the value of I is obtained, the space will be accessed, the result is the changed value. C ++ regards const as a constant, and the compiler will replace the reference to I directly with the constant, for example, cout <I; will be understood as cout <10; it does not access the memory address of I to retrieve data. It is a bit like the macro # define I 10 in the C language. Therefore, in C ++, I will output 10, and * P will output 20.
For problem 2, c ++ only replaces the built-in data types with constants, but not the built-in data types such as struct. Because the struct type is not a built-in data type, the compiler does not know how to replace it directly. Therefore, you must access the memory to retrieve data, and access to the memory to retrieve data will inevitably get the value after the Q pointer changes, therefore, the processing mode is completely different from the const int type in C ++.

Summary
There are many differences and relationships between C and C ++. const is only one of them. Understanding and using const is the foundation of programming and requires a good understanding.

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.