Knowledge of C ++ const

Source: Internet
Author: User

Address: http://blog.sina.com.cn/s/blog_6ccd0a1101017zqu.html

1. The initial motivation of const is to replace the Preprocessor # define for value substitution.


# Define only performs some text substitution. It does not have the type check concept or the type check function. Therefore, the value substitution of the pre-processor may cause some problems. These problems can be avoided by using const in C ++.


2. Const in C ++ is an internal connection by default (Internal Linkage)
Const is visible only in the defined file, and cannot be seen by other compilation units during connection (default ).


3. When defining a const, a value must be assigned to it.
That is to say, Initialization is required during definition.


4. const is either saved in the symbol table or allocated to it by the compiler
Generally, the C ++ compiler does not create a bucket for const, but stores the definition in the symbol table.
You can create a bucket for the const in either of the following cases: 1) perform the operation to retrieve the const address (including passing the const to a function with reference parameters) 2) the const is defined as extern. In this case, it is used in other compilation units. Therefore, it must use a bucket.


5. Const during compilation
Const is a const during compilation and can be used during compilation.
For example, a const int is used to determine the size of another array:
Const int bufsize = 100; Char Buf [bufsize];
Note: This code is correct in C ++, but is wrong in C. Const in C means "a common variable that cannot be changed". Const always occupies storage in C. The C compiler cannot regard const as a constant during compilation.

In the following situations, const cannot be used during compilation: 1) its value is unknown during compilation. For example:
Const char c = cin. Get ();
The value of C is unknown during compilation, which means that a bucket is required and the compiler does not keep any information about it in the symbol table. Therefore, C is unavailable during compilation.
2) When const is used for a set (array, struct, etc. For example:
Const int I [] = {1, 2, 3, 4 };
The compiler is not complicated to save a set to the symbol table, so memory must be allocated. In this case, const means "a bucket that cannot be changed", but cannot use its value during compilation. For example, the following statement is incorrect for I:
Float f [I [3];
Here we try to use the elements in I to determine the size of another array, and then I cannot be used during compilation. Therefore, the compiler reports an error here.


6. External Reference of const
Since const in C ++ is an internal connection by default, a const cannot be defined in one file, and it is referenced as an extern in another file. To make const an external connection so that another file can reference it, you must explicitly define it as extern, as shown below:
Extern const int x = 1; (1)
By initializing it and specifying it as extern, we force it to allocate memory. Initialization makes it a definition rather than a declaration. The following statement:
Extern const int X; (2)
Is the declaration of the const in another file, indicating to reference a const variable X in other files.
Note the differences between statements (1) and (2). (1) define const; (2) Declare const in another file, to reference the const variable X in other files.
Note that, as mentioned in 4, when a const is defined as extern, it will allocate storage space for it. Then, the use of the const will not pass through the symbol table, but through the address of its bucket.


7. constant folding)
When a const is not allocated to a bucket by the compiler, the compiler simplifies a complex constant expression through necessary computation during compilation. This operation is called constant folding"
For example: Const int I = 10*5;
The compiler stores information similar to "I = 50" in the symbol table. This is what # define cannot do. The pre-processor simply replaces the text.


8. Const and pointer
1) pointer to const (Initialization is not required ):
Const int * U; OrInt const * V;
U is a pointer pointing to a const Int.
2) const pointer (initialization required ):
Int d = 1; Int * const W = & D;
W is a pointer, which is a const pointer to the int.

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.