To do function parameters:
For example, the value of the fun (const int * i) parameter I cannot be modified during the fun () function execution, and it will keep the value passed in when this function is called. If the statement attempting to modify the I value appears in the Fun () function, it will cause the program to fail to compile, thus protecting the parameter I.
Note: const is commonly used to restrict pointer parameters to functions, reference and array parameters, and general form parameters because the parameter and argument are not variables of the same memory unit, so modifications to the formal parameters do not affect the arguments, so there is no need to restrict the function body from modifying the parameters .
To do a function return value:
The function return value is const only if the function is returned as a reference. The reference to the function return value is reference to the relevant knowledge here
A function return value reference constant indicates that a function call expression cannot be used as an lvalue .
For example, Function min ().
int & min (int &i, int &j);
You can assign a value to a function call because it returns an lvalue: Min (A, b) = 4;
However, if the return value of the function is limited to const: const int & min (int & i, int &j);
Then, the Min (A, B) call cannot be assigned.
Const function:
In a class, you can define the following form of member functions for a class:
Class ClassName {
int member;
Public
int getmember () const;
};
Here, the const that is appended to the function definition header indicates that the function is a "read-only function" that can be referenced, but cannot alter the data members in this class.
If this is not the case in the function body:
ClassName:: GetMember ()
{member = 4;
return member;
}
In addition, theconst member function cannot invoke other non-const functions in the function.
For a more detailed reference here
Summarize:
At first just want to understand the const do function parameters, see more than half an hour, the final summary of a bit, in many of the code to explain the const, although not difficult to understand, but it is difficult to remember, at this time can not be anxious, do not have to hard to remember, just hit a face-to-face, in the practical application in the
Const Summary: Do function parameters, do function return value, and const function