CONST foundation
If the const keyword does not involve pointers, we understand that the following is the case involving pointers: 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 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 ( );
Two Const initialization
Let's take a look at the case of Const variables 1) Cases of non-pointer constant constant initialization: a b; const a a = B;
2) 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 that declares Const, and cannot access a general member function;
[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;
Three Const modifiers as parameters and return values
In fact, whether the parameter is still the same, the truth is the same, when the parameter is introduced, and the function is returned, the constit variable 1 modified parameters is initialized, 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, and the shape is referred to Const A * a, the content of the passed pointer cannot be performed. Change, protect the content points to the original pointer; if the shape is referred to Const A & A, the properties 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, the case where the CONST modified return value is the object itself (non-reference and pointer) is used for the bull operator overload function and generates a new object. [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);
Use of Const in the four-class member function
After the function is placed, the shape is like: void fun () const; if a member function does not modify the data member, then it is best to declare it as const, because the data member is not allowed in the Const member function, if Modify, the compiler will report an error, which greatly improves the robustness of the program. Some suggestions using const
1 To use const, this will bring you endless benefits, but the premise is that you have to figure out the original committee; 2 To avoid the most general assignment operation error, if you assign the const variable, you can see questions; 3 in parameters Use const should use references or pointers, rather than a general object instance, the reason is the same; 4 Consts three usage (parameters, return values, functions) in member functions (parameters, return values, functions) should be used well; 5 Do not easily use functions The return value type is set to const; 6 Do not specify the return value type in addition to the overloaded operator, the return value type is customized to a const reference to an object;
I have limited level, welcome criticism, you can contact Kangjd@epri.ac.cn [Thinking Question Answer] 1 This method is incorrect, because the purpose of the declaration pointer is to change the content you point to, and the pointer e pointed out It is a constant, so it is incorrect; 2 This method is correct because the content points to the pointer points; 3 this practice is incorrect; in Const A :: Operator = (Const A & A), in the parameter list Const's 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 constant to A Quote, the CONST constant cannot be assigned again.