The const keyword can not only modify variables, but also modify the parameters, return values, and even definitions of functions.
1. Modify Variables
Const char var; // It indicates that the VaR variable is now "Read-Only variable", and any statement modified on the VaR is regarded as an error by the compiler.
Of course there are some similar methods: Char const * P;
Char * const P;
Const char * const P;
2. Modify function parameters
If a parameter is used for output, No matter what data type it is, or whether it uses "pointer transmission" or "reference transmission", it cannot be modified by const, otherwise, the output function is lost. Const can only modify the input parameters:
If the input parameter uses "pointer passing", adding const can prevent accidental changes to the pointer and play a protective role.
For example, the stringcopy function:
Void stringcopy (char * strdestination, const char * strsource );
Strsource is the input parameter and strdestination is the output parameter. After the const modifier is added to strsource, if the statements in the function body attempt to modify the content of strsource, the compiler will indicate an error.
If the input parameter uses "value transfer", because the function will automatically generate a temporary variable for copying this parameter, the input parameter does not need to be protected, so do not add const modification.
For example, do not write the void func1 (int x) function as void func1 (const int X ). Similarly, do not write the void func2 (A) function as void func2 (const ). A is the custom data type.
For parameters of non-Internal data types, functions declared like void func (A) are destined to have relatively low efficiency. Because a temporary object of type A is generated in the function body for copying parameter A, the construction, replication, and destructor of the temporary object will consume time.
To improve efficiency, you can change the function declaration to void func (A & A) because "reference transfer" only uses the parameter alias and does not need to generate a temporary object. However, the void func (A & A) function has one disadvantage:
"Reference transfer" may change parameter A, which is not expected. It is easy to solve this problem by adding the const modifier, so the function eventually becomes void func (const A & ).
Similarly, should void func (int x) be rewritten to void func (const Int & X) to improve efficiency? It is completely unnecessary because the internal data type parameters do not have a structure or structure process, and replication is also very fast. The efficiency of "value transfer" and "reference transfer" is almost the same.
3. the return value of the const modifier Function
If the const modifier is added to the return value of a function in the "pointer passing" mode, the content of the function return value (that is, the pointer) cannot be modified. The return value can only be assigned to the same type pointer with the const modifier. For example, Function
Const char * getstring (void );
The following statement causes a compilation error:
Char * STR = getstring ();
Which of the following statements is true?
Const char * STR = getstring ();
If the function return value adopts the "value transfer mode", because the function copies the return value to an external temporary storage unit, adding the const modifier has no value.
For example, do not write the int getint (void) function as const int getint (void ).
Similarly, do not write function a geta (void) as const A geta (void), where A is the custom data type.
If the returned value is not an internal data type, it is indeed highly efficient to rewrite function a geta (void) to const A & geta (void. At this time, you must be careful to find out whether the function is to return a copy of an object or only return an alias. Otherwise, the program will fail.
There are not many cases where function return values are passed by reference. This method is generally only used in class assignment functions to achieve chained expression.
For example:
Class
{
A & operate = (const A & other); // value assignment function
};
A A, B, C; // A, B, C is the object of
A = B = C; // normal chained assignment
(A = B) = C; // The chain assignment is invalid, but valid.
If you add const to the return value of the value assignment function, the content of the returned value cannot be modified. In the preceding example, statement a = B = C is still correct, but statement (a = B) = C is invalid.
4. Const member function (the function of const indicates that it will not modify data members)
Any function that does not modify data members should be declared as the const type. If you accidentally modify the data member when writing the const member function, or call other non-
Const member function, the compiler will point out errors, which will undoubtedly improve the robustness of the program. In the following program, the stack-like member function getcount is only used for counting,
Logically, getcount should be a const function. The compiler will indicate errors in the getcount function.
Class Stack
{
Public:
Void push (int elem );
Int POP (void );
Int getcount (void) const; // const member function
PRIVATE:
Int m_num;
Int m_data [100];
};
Int Stack: getcount (void) const
{
+ + M_num; // compilation error. An attempt is made to modify the data member m_num.
Pop (); // compilation error in an attempt to call a non-const Function
Return m_num;
}
The const member function declaration looks strange: the const keyword can only be placed at the end of the function declaration, probably because it is occupied elsewhere.
Rules for const functions:
A. the const object can only access the const member function, but not the const object can access any member function, including the const member function.
B. Members of the const object cannot be modified. However, objects maintained by the const object through pointers can be modified.
C. The const member function cannot modify the object data, regardless of whether the object has the const nature. during compilation, it checks whether the object modifies the member data.
D. However, the data member with the mutable modifier can be modified by any means in any situation. Naturally, the const member function can be modified at this time.