Constants are constant constants that provide security and controllability in C ++ programming. A constant is identified by const. One of its functions is to replace macro replacement. Const provides a strict type check for values. Compared with macro replacement, const provides powerful security assurance.
Const is usually used to modify the function parameters as pointers and references, and can only modify the input parameters. If the input parameters use the value transfer method, because temporary variables are automatically generated during function calling for copying real parameters, the original parameters are not modified, so you do not need to use const for modification.
The cosnt-modified part of the function body is constant. If the parameter is const type * parameter, the content of the passed pointer cannot be modified, this method is used to protect the content pointed to by the original pointer. If the parameter is const
Type & parameter, the passed referenced object cannot be changed, that is, the object protects the source object.
We recommend that you do not use the value transfer method for non-Internal data type input parameters, but use the "const
Reference object "transfer method, to improve efficiency. If the input parameters are of the internal data type, we recommend that you do not change the value TRANSMISSION METHOD
Reference transfer mode.
The following describes how the function return value is of the const type.
If the return value of a function is a constant, it means that the original variable cannot be modified. If the return value is used, the return value is an internal data type. This function declaration is meaningless.
As follows:
Int function0 ()
{Return 0 ;}
Const int function1 ()
{Return 0 ;}
Void main ()
{
Const int A = function0 ();
Int B = function1 ();
}
Compile and run the above program. It can be seen that for internal data, whether the returned value is a cosnt has no effect, because this function declaration has no significance, this is not recommended. For non-Internal data types, the situation is different. If a Class Object const constant is returned by a function, the return value is protected, that is, the return value cannot be used as the left value.
Const can be used to declare a class member function, as follows:
Void function () const {}
The modified member functions cannot modify member variables. If a statement modifies a member variable, compilation errors will occur, greatly improving the robustness of the program.