Const usage summary (original)

zhaozj2021-02-16  90

CONST usage

Author: INDUSTRY UPON (reproduced please specify)

The usage of Const keywords in C is very flexible, and use const will greatly improve the robustness of the program, refer to the use of the Const use of Kang Jian Dong brother, some additions to it, wrote this article.

1. Const constant, such as const Int max = 100; advantage: The constant constant has a data type, while the macro regular amount has no data type. The compiler can perform type security checks for the former, and only characterize only characters, there is no type of security check, and the unexpected error may occur when character replacement (marginal effect)

2. Data members of Const modifiers. Such as: Class A

{

CONST INT Size;

...

}

Const data members are constants only in an object survival, and they are variable for the entire class. Because classes can create multiple objects, different objects of different objects can be different. So you can't initialize the Const data member in the class declaration, because the compiler does not know what the value of the Const data member is not found. Such as

Class A

{

const Int size = 100; // Error

INT Array [size]; // error, unknown SIZE

}

Initialization of Const data members can only be performed in the initialization table of the constructor of the class. To build constant constant in the entire class, you should implement the enumeration constant in the class. Such as

Class A

{...

ENUM {size1 = 100, size2 = 200};

Int Array1 [Size1];

Int array2 [size2];

}

The enumeration often does not take up the storage space of the object, and they are fully evaluated when compiling. However, the implicit data type of the enumeration is an integer, its maximum value is limited, and cannot represent floating point numbers.

3. The case of const modifying pointers, see the following formula:

INT b = 500; const * a = & [1] int const * a = & [2] int * const a = & [= & [3] const * const a = & [4] If you can distinguish the above four The situation, then, congratulations, you have taken a welcome step. I don't know, it doesn't matter, we can refer to the practice of "Effective C " Item 21, if constant is on the left side of an asterisk, then const is the variable pointed to the pointer, that is, the pointer points to constants; if const is in an asterisk On the right side, Const is the modified pointer itself, that is, the pointer itself is constant. Thus, [1] and [2] are the same, all of which are pointed to by the pointer (constant is not related to the location of the variable declaration), and the content is not allowed to change the operation, if not * a = 3; [3] The pointer itself is constant, and the content points to the pointer is not constant. In this case, the pointer cannot be changed, such as a is wrong; [4] is the pointer itself and the pointing content For constant.

4. Const initialization first look at the case of Const variable initialization 1) Cases of non-pointer constant constant initialization: a b; const a a = b; 2) Pointer CONST constant initialization:

A * D = new a (); const a * c = d; or: const a * c = new a (); 3) Reference CONST constant initialization: a f; const A & E = f; // This e can only access a function of declaring constant, not accessing general member functions; [Reflections 1]: Is this assignment method correct? Const a * c = new a (); a * e = C; [Reflections 2]: Is this assignment method correct? A * const c = new a (); a * b = C;

5. Some of the powerful features of Const lies in its application in the function declaration. In a function declaration, const can modify the return value of the function, or a parameter; for a member function, it is also modified to be a whole function. There are several situations, the following will gradually explain: a & operator = (const A & a); void fun0 (const A * a); void fun1 () const; // fun1 () is a class member function const a fun2 ( );

1) Const, such as Void Fun0 (Const A * a); Void Fun1 (Const A * a); When the function is called, the constant constant is initialized with the corresponding variable, in the function body, according to the part modified by Const Perform constant quantization, such as constit a * a, you cannot change the content of the passed pointer, protect the content points to the original pointer; if the shape is referred to Const A & A, it cannot be passed from the reference object. Make changes to protect the properties of the original object. [Note]: Parameter const is usually used for the case where the parameter is a pointer or reference, and can only modify the input parameters; if the input parameter is "value passed" mode, since the function will automatically generate temporary variables to copy this parameter, this parameter It doesn't matter if you need it, so you don't have to modify.

[Summary] For input parameters of non-internal data types, the purpose is to improve efficiency because it is changed to "Const References" in the way. For example, change Void Func (a a) to Void Func (Const A & A)

For input parameters for internal data types, do not change the "value to" to "Const reference". Otherwise, it will not only improve the efficiency, and the understandability of the function is reduced. For example, Void func (int X) should not be changed to Void Func (Const Int & X)

2) Const a fun2 (); const A * fun3 (); this declares that the constrail is decorated in accordance with the "Modification Principles" after the return value is declared. Const Rational Operator * (Const Rational & lh, Const Rational & RHS) {Return Rational () * rhs.Numerator (), lhs.Numerator () * rhs.denominator ());} Return Value CONST modification can prevent Allow such operations: Rational A, B; RADIONAL C; (a * b) = C; generally use const modified return values ​​for the object itself (non-reference and pointer), mostly used for bull operator overload functions and generate new objects when. [to sum up]

1. In general, when the return value of the function is an object, if it is declared as const, it is used to overload the operator. Typically, it is not recommended to use the const modified return value type as an object or a case referenced to an object. The reasons are as follows: If the return value is a reference for a const (const A test = a instance), or a reference to a reference is const (const A & test = a instance), the return value has a const attribute, then the return instance can only be accessed The public (protected) data member and the Const member function in class A, and does not allow assignment operations, which are rarely used in general. 2. If a function returns a value to a "pointer delivery" method, the content returns (ie, the pointer) can not be modified, the return value can only be assigned to the same type of CONST modified. Such as:

Const char * getString (void);

The following statement will appear compilation:

CHAR * STR = getString ();

The correct usage is:

Const char * str = getString ();

3. Function return value is not much in the case of "reference delivery", which typically only appears in the 赙 value function of the class, and the purpose is to achieve chain expression. Such as:

Class A

{...

A & Operate = (const a & other); / / negative function

} A a, b, c; // a, b, c is a object

...

A = b = c; // Normal

(a = b) = C; // is not normal, but legal

If the return value of the negative value is plus const modification, the content of the return value is not allowed to modify, and the A = B = c is still correct in the above example. (a = b) = c is incorrect. [Think 3]: Is this defined assignment operator overload function? Const A & Operator = (Const A & A);

6. The use of Const in the class member function is generally placed behind the function body, such as: void fun () const; any function that does not modify the data is due to the state of the constant. If you write a Const member function, you accidentally modify the data member, or call other non-Const member functions, the compiler will report an error, which greatly enhances the robustness of the program. Such as:

Class Stack

{

PUBLIC:

Void Push; INT ELEM

INT POP (Void);

INT getCount (void) const; // constist member function

Private:

INT m_num;

INT M_DATA [100];

}

INT Stack :: getCount (void) Const

{

m_num; // Compiling errors, attempt to modify data members m_num

POP (); // Compiling errors, trying to call non-Const functions

Return m_num;

}

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

New Post(0)