[C + +] Const detailed

Source: Internet
Author: User

/**************************************************************** Primary Understanding: 1.const is defined as constant = = "Const means read-only 2. The following const variables must be initialized! ***************************************************************/    /*--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------------Reason analysis: const in C language is in the form of allocating memory!                In this way, it is easy for the pointer to take advantage of C language, the const variable is a read-only variable, has its own storage space Const in C + + does not allocate memory, but placed in a symbol table, the list is: Key value Compile process if you find that the use of constants is directly in the symbol table value substitution---the compilation process if found to use the constextern or &Operator
Assign a memory space (compatible with C) to the corresponding constant (such as the constant 10 in the following symbol table a), and then point the pointer variable p to this memory spaceConst constants in C + + A. May not allocate storage space ... B. Storage space may also be allocated 1). When the const constant is global, and you need to use 2 in other files). When using the & operator to take the address of a const constant

The difference between const and define C.1 Const constants is determined by thecompilerprocessing, providing type checking and scope checking
The C.2 macro is defined by thePreprocessorProcessing, simple text substitution

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------------*/#include<iostream>#include<cstdio>#include<cstdlib>extern Const intTest = One;//Test if you want to share in another file, you must add extern for both the declaration and the definitionstructteacher{Charname[ -]; intAge ;}; intMainvoid) {//const int A;//equivalent to int const A; Represents a constant shaping number a// //const int * C;//c is a pointer to a constant number of shapes (the memory data pointed to cannot be modified, but can be modified by itself)// //int * Const D;//d is a constant pointer (pointer variable cannot be modified, but it points to the memory space can be modified)// //Const int * Const E;//e A pointer to a constant shape (the pointer and the memory space it points to cannot be modified) Const intA =Ten; int*p = (int*) &a;//The const int* must be artificially converted to int*//Take 10 out of the symbol table A to create a separate memory space and point p to this memory spaceprintf"Original A =%d\r\n", a); *p = One; printf ("Modify a =%d\r\n", a);//The actual print is a in the symbol table.printf"Modify *p =%d\r\n", *p); printf ("The same code, if placed in the C compiler, is 10 11 and one. \ r \ n"); return 0;} /*two applications for ———————————————————————————————————————————————— const ———————————————————————————————————————————— ————*/ /*pointer variable name, member name = = (* pointer variable name). Member Name*/intFunConst Teacher * p) {// When const modifies a parameter, the memory space to which the pointer is not modified can be reduced by using the parameter//p->age = ten;//"Err"//p = NULL;//"OK"}intFuncTeacher * Const p) {// When const modifies a parameter, the pointer itself cannot be modified using the parameter, but the memory pointed to can be modified //p->age = ten;//"OK"//p = 0;//"Err"}

[C + +] Const detailed

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.