Some places should be noted when using const

xiaoxiao2021-03-06  100

We often use it, but sometimes don't pay attention to the program is abnormal, and you still don't know what is going on, some are useful to pay attention to:

1. When the function return value is const, the returned thing is assigned to a type identical indicator;

2. Int defined with const can be used to open an array, but the elements in the constant array of const are not used to define an array.

3. Const Int * i; int * const * i; int * const i; the first two functions are the same, indicating that the content points to the i is unchanged; the last description pointer points to the address constant, but the content varies.

4. The const member function in the class is defined as a CONST after the prototype. Constant functions cannot modify any properties in the class. But there are two ways to modify.

1) {(Yourclass *) this-> member = value;

2) Define a member to Mutable can be modified by a constant function.

5. Constant constants in the class, cannot be used in the class to define an array. ENUM {One = 100; TWO = 2}; defined one, TWO can be. Usually an ENUM defined setup problem: enum a {l = 9, z}; at this time, the value of Z is 10.

转载请注明原文地址:https://www.9cbs.com/read-112623.html

New Post(0)