The const modifier for C + + member functions is divided into three types: 1. modifier parameters; 2. modifier return value; 3. Modifies the this pointer. Briefly describe the following points of knowledge, and later find Kung Fu perfect.
1. Modification of function parameters.
1) const can only be used to modify input parameters. Output type parameters cannot be modified with Const.
2) If the input parameter is "pointer passing", then the const modifier prevents accidental changes to the pointer, which can be used as a protective function.
3) If the input parameter takes "value pass", the function will produce a temporary variable (local variable), copy the value of the parameter and press into the function stack. When using this parameter in a function, the value of the temporary variable in the function stack is accessed, and the original variable is not protected, so do not add a const modifier.
4) parameters of the basic variable type are input parameters for "value passing", and no reference is required. The custom variable type (class type, struct type) parameter is used as an input parameter for "value passing", preferably in the "const+ reference" format, void func (const a& A). The reason is that when a custom variable type is passed as a value, the design creates temporary variables, constructs, replicates, and destructors, which are time consuming.
Consider the reason from the basic principle of the function stack. We know that when a function is called, it creates a temporary variable for each argument that is created and presses it into the function stack. In the case of a basic variable type, a copy of the argument is stored in a temporary variable that is pressed into the function stack, and if it is a custom variable type, the instance of the type is created on the heap, the argument is copied onto the heap, and the address of the instance on the heap is pressed into the function stack; A copy of the pointer address (which can actually be thought to be the basic variable type of the variable that holds the pointer) presses into the function stack.
That is, the function stack either holds a copy of a basic type parameter or is a top-level pointer. For a parameter saved on a function stack, a copy of the argument can be used as a normal local variable, a value can be modified, and for a pointer variable, it can be treated as a top-level pointer, and its value cannot be modified, but the value it points to can be modified.
Therefore, for the basic variable type, the function internal operation is the copy on the function stack, does not affect the original value, for the class type (non-pointer input parameter), the operation is also the function stack address point to the instance copy, also does not have the influence to the original value, but for the pointer, The inside of the function cannot change the pointer value held by the pointer variable (the pointer is the top-level pointer), but the pointer points to the address of the original value, so the original value can be modified.
For reference, a) reference is simply an alias of a variable, a reference to a meta-variable memory address, no new memory allocations and a copy of the variable, b) The declaration must be initialized immediately after the reference, C) the reference once defined, cannot change its value, that is, no longer as a reference to other variables; d) The original variable can be fully manipulated by reference.
As you can see, when a variable that occupies a large space is used as an input argument, it is well suited to pass by reference. Because it is passed by reference, it simply passes an alias of the variable itself, and the memory allocation, construction, assignment, and destruction of the new variable are not done.
If the argument is not allowed to be changed in the function, then a const adornment should be added to the reference parameter.
Based on the above considerations. When const modifies an input parameter, it is only necessary to decorate the pointer type and reference type (although not mandatory, but it should be a habit for the input pointer or reference parameter to be decorated with a pointer).
At the same time, this also shows that a programming should be developed in the habit, for the input parameters, should be at the beginning of the function to define a local variable to receive the input parameter, rather than directly use.
2. Adornment of the return value.
This application is relatively small. Most of the return values take the time "value pass". If the return value is modified to const, then the variable that receives the return value must also be defined as Const.
3. Modify the This pointer.
We know that C + + member functions, at compile time, pass in a this pointer, pointing to the instance itself. The this pointer is actually a top-level pointer by default. That is, if there is a classa, then this pointer is actually similar to the following definition:
ClassA * const this;
That is, the this pointer points to the instance itself and is not modifiable, but can be modified by the this pointer to the member variable it points to. The member variable m_var is accessed within the member function, in fact in the following form:
This.m_var;
If we do not want to modify member variables when we design a member function, we should define the this pointer as the underlying pointer. C + + is defined by adding a const after the function signature, that is,
void func (const a& A, int b, const int* C, int* D) const;
Obviously, in the above member function, A is a const reference pass, can not change the original value, B is the value of the pass, C is a const pointer passed, cannot change the original value, D is the output parameter, you can change the original value. The function is a const member function, and the member variable value cannot be modified.
here are some points to note for the const member function :
1) const objects can access only const member functions, and non-const objects have access to arbitrary member functions, including const member functions. For Class A, there are
const A;
Then a can only access the const member function of a. And for:
A b;
B can access any member function.
2) The Const object's member variable cannot be modified.
3) mutable modified member variable, can be modified under any circumstances. That is, the const member function can also modify a member variable that is mutable decorated. C + + very shit place is mutable and friendly such characteristics, very chaotic.
4) The const member function can access const member variables and non-const member variables, but cannot modify any variables. The check occurs at compile time.
5) Non-const member functions can access non-const data members of non-const objects, const data members, but cannot access arbitrary data members of Const objects.
6) The const member function is used only for non-static member functions and cannot be used for static member functions.
7) The const modifier of the const member function is not only added to the function declaration (including inline functions), but also defined outside the class.
8) as a good programming style, when declaring a member function, if the member function does not modify the data member, the member function should be declared as a const member function whenever possible.
Modification of C + + member functions by the const keyword