C ++ programming ideology learning notes-constants

Source: Internet
Author: User

C ++ programming ideology learning notes-constants

1. Use the global const
1. The use of const has two effects: one is to generate a compile-time constant and the other is a runtime constant.
As a compilation constant, it is mainly used as a value substitution, that is, to replace the # define constant definition in C, which is generally used to set the array size.
In this case, no storage space is allocated. The space is allocated only when there is an extern limit or an address is obtained.
As the main control program of constants during runtime, they will be allocated space, but once initialized, they cannot be changed,
Arrays, structures, and other sets are always runtime constants and cannot be used as compilation constants. For example, they cannot be used to set the array size.
In addition, const in C is only a constant during runtime.
2. Use of const pointer
Pointer to const: const int * X, int const * X;
Const pointer int * const X;
Note: The const pointer (reference) cannot be assigned to a non-const pointer (reference) (it will be changed through a non-const pointer
Change the const pointer to the value !), However, you can assign a non-const pointer (reference) to the const pointer (reference ). This is based on
Security considerations.
3. const is used for function parameters and return values.
As a const value, a parameter is generally used as an initial value in a function. However, it is generally easier to understand it as follows:
Void F (INT ic)
{
Const int & I = ic;
// Others
}
Parameters are considered as const pointers or references for security and efficiency reasons. Generally, a function that does not change parameters must be used.
Const int * cip or const int & ci; such a function can be a const and a non-const parameter, if the function parameter does not have const
The modifier can only accept non-const parameters, but cannot accept const parameters. It is not universal, so remember the following usage:
Void f (const int & ic)
{}
Note: All temporary variables are constants;
When the return value is a const value, it indicates that the return value cannot be left or modified. It is generally used for user-defined classes.
Data is meaningless.
When using the return value as the const pointer, note that the variable that accepts the return value must be modified by the const.
Ii. Use of const in the class
1. Meaning of const in a class-it allocates storage space in each class object and cannot be changed once initialized (
To be initialized in the initialization table of the constructor.
2. To use a compile-time constant within a class, you can use the enum enumeration, which does not occupy storage space.
3. Use const for member functions-indicates that such functions do not change the data of objects and can be called by the const object (the const object only
Can call the const member function) But if you want to change data in the const member function, there are two methods:
1> use the this pointer to forcibly convert the const variable to a non-const variable.
2> modify the variable to be changed with mutable.
2> is recommended.
Iii. Use of volatile
Indicates that this data can be changed-this is used by the compiler to optimize this data. When the compiler reads this data, it considers that it has
If it is changed, it will be read to the register again, and it will not be assumed that it has not changed so that the Register value is directly used.
Const volatile indicates that this object cannot be changed by programmers, but it can be changed through external tools (? Does not understand ).

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.