The usage of Const keywords in C is very flexible, and use const will greatly improve the robustness of the program, and some of the people will summarize the following, expect to help everyone: a const base If the const keyword does not involve the pointer We understand that the following is the case involving the pointer: int b = 500; const INT * a = & b; [1] int const * a = & b; [2] int * const a = & b; [3] Const INT * const a = & b; [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 the application of 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 ( );
Two Const initialization first look at the case of Const variables 1) Cases of non-pointer constant constants: 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 E can only access a function of declaring constant, not access 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 the parameters and the return value are actually, whether the parameter or return value is the same, when the parameters are incompatible, when the function is returned, the constituent 1 modified parameters 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, and in the function body, constant quantization is performed according to the part modified in Const, such as const a * a, Then change the content of the passed pointer to protect the content points to the original pointer; if the shape is referred to Const A & A, the attributes of the original object cannot be changed to the CONST A & A. [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. 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. [Think 3]: Is this defined assignment operator overload function? Const A & Operator = (Const A & A); Using Const in the four-class member function is generally placed behind the function body, such as: void fun () const; if a member function does not modify the data member, then it is best to The declaration is constant because the Const member function is not allowed to modify the data, if modified, the compiler will report an error, which greatly enhances the robustness of the program.
5 Some suggestions for using Const 1 To use const, this will bring you endless benefits, but the premise is that you must figure out the original committee; 2 To avoid the most general assignment operation error, if you assign the consT variable, specifically visible Thinking questions; 3 Use const in parameters Should use references or pointers, rather than a general object instance, why: 4 Const in member functions (parameters, return values, functions) should be used well; 5 Do not easily set the return value type of the function to const; 6 In addition to the overloaded operator, do not specify the return value type as a Const reference to an object;
I have limited level, welcome to criticize, you can contact Kang_JD@163.com [Thinking Question Answer] 1 This method is incorrect, because the purpose of the declaration pointer is to change the content to which it is pointed, and the pointer E of the statement is A constant, so incorrect; 2 This method is correct, because the content pointed to by the declaration pointer is variable; 3 this practice is incorrect; in Const A :: Operator = (Const A & A), the parameter list in the parameter list Usage is correct, and when this continuous assignment, the problem will appear: A a, b, c: (a = b) = C; because the return value of a.Operator = (b) is a const reference to A. The CONST constant cannot be assigned again.