CONST usage

xiaoxiao2021-03-05  28

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 (marginal effect) 2. of the Const modifier class is generated when character replacement. Such as: Class A {const Int size; ...} Const data member is only constant within an object survival, but is 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} Const data member initialization can only be performed in the initialization table of the class constructor. 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];} 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. Constly modifying the pointer, see the following formula: int b = 500; const INT * a = & [1] int const * a = & [= & [2] int * const a = & [3] const * const a = & [4] If you can distinguish these four situations, 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. Constimation first look at the case of Const variables 1) Cases of non-pointer constant constant initialization: a b; const a a = b; 2) Pointer 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 way e can only access the declaration of const And not accessing general member functions; [Thinking 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 powerful features of the 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 CONST constant is initialized with the corresponding variable, and in the function body, according to Const modification The part is constant, such as constit a * a, the content of the transferred pointer cannot be changed, protecting the content points to the original pointer; if the shape is referred to Const A & A, it cannot be passed The reference object 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 "value" 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) constraging return value const, such as const a fun2 (); const A * fun3 (); this declares that after returning, const follows " The modification of the principle is modified to protect the corresponding protection.

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. [Summary] 1. Under normal circumstances, 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 error: char * str = getString (); the correct usage is: const char * str = getString (); 3. Function return value adopts "reference delivery" There are not many occasions, which typically only appear in the 赙 value function of the class, the purpose is to achieve chain expression. Such as: Class A {... a & operate = (const A & other); / / negative value function} A A, B, C; // a, b, c is an object A = b = c; // Normal ( A = b) = c; // is not normal, but legal if the returns of the negative value plus const modifications, then 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. After the use of Const in the class member function is generally placed behind the function, such as: void fun () const; any function that does not modify the data member is due to this declaration as a const type . 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.

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

New Post(0)