Coding phase, many people do not pay much attention to the use of const, think dispensable, if you develop a simple system, if you can remember the meaning of all variables, if you are a person can control the use of all variables, if your software released after the release does not need more maintenance work, Then you can forget the const, otherwise, const is a powerful tool for communication between multiple programmers.
First, the most basic use of const is to identify constants so that the compiler can detect incorrect use of the variable, or you can enable other programmers to use this constant, and note that you cannot change this constant. The const appears in different places and has different meanings.
√const int number = 1; The variable is constant and cannot be assigned to it later, so you must assign an initial value here
√const int * Pnumber; A pointer to a constant (*pnumber) value cannot be changed, but the value of the pointer pnumber can be changed
√int * Const Pnumber = & number2; A constant pointer to a variable, the value of the pointer cannot be changed, so you must first initialize the
√const int * Const PNUMBER = & number; Constant pointers to constants, constants, and values of pointers cannot be changed, so you must first initialize the
√const int & number1 = number; Reference to a constant, all references in C + + are constant references, a reference to a variable can no longer change the value to point to another variable, so whether or not the reference is a constant reference, you must assign an initial value when initializing
Second, C + + 's arguments for functions do not differentiate between input and output parameters, and often because a return value is not sufficient, we pass several references or pointers to receive the return value. We can use const to indicate that a parameter is an input parameter, and the function does not change the value of this parameter, as in the following function:
BOOL Copyclass (Value & dest, const value & SRC);
Among them, the value for the user-defined class, the function of the Copyclass function for copy src content to dest,src for input parameters, its value can not be changed, dest for output parameters, other programmers according to the definition of function, can have a clear understanding of function function.
Next, let's take a look at the last use of Const. Consider the following implementation of the Copyclass function and the value class:
class Value {
public:
int GetValue() { return _value; }
private:
int _value;
}
bool CopyClass(Value & dest, const Value & src) {
int v = src.GetValue();
…………
}
Excuse me: Can this code be compiled? Test, there is a compilation error, the error is now the following statement:
int v = src. GetValue (); Compilation error, because SRC is a reference to a constant, the compiler cannot determine whether the GetValue function changed the content of SRC, so it is forbidden to call
So, can we just give up using the const ID to enter parameters? No, we have another way, as long as the definition of the GetValue function can be changed as follows:
int GetValue () const {return _value}//const means no change to the content (that is, the value class) that is directed to the this pointer passed to it
With this change, the code can be compiled, and other programmers can quickly learn that the member function simply outputs the contents of the class object and does not alter any of the object's contents.