Const use details

zhaozj2021-02-12  135

Detailed use of const: Kang Jiandong usage of const keyword in C is very flexible, and the use const will greatly improve the robustness of the program, now some of my experience are summarized below, hope to help everyone: If the basis of a const const Keywords do not involve pointers, we understand that the following is the case involving pointers: int b = 500; const INT * a = & [1] int const * a = & [2] int * const a = & [ 3] Const Int * 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. 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 ( ); Interpreter of CONST first look at the case of Const variable initialization 1) Non-pointer constant constant initialization: a b; Const a a = b; 2) Pointer (reference) Const constant initialization: a * d = new A (); const A * c = d; or: const a * c = new a (); reference: a f; const A & E = f; // This can only access a function of declaring constant, not Access a general member function; [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; three const modifiers as parameters and return values ​​are actually, whether the parameters or return values, the truth is the same, when the parameters are incompatient, and the function is returned. Initialize the const variable 1 modified parameters 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 The modified portion is constant, such as the shape of the Const A * a, can not change the content of the transferred pointer, protect the content points to the original pointer; if the shape is involved in Const A & A, it cannot be passed The reference object changes to protect the properties of the original object. [Note]: Parameter const usually uses the case where the parameter is a pointer or reference; 2 modified the return value const, such as const a fun2 (); const a * fun3 (); this declares that after the return value, const is "Modification principle "Conductive, which plays a 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] Under normal circumstances, when the return value of the function is an object, if it is declared as const, it is used for the overload of 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.

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

New Post(0)