See
Const
Keyword,
C ++
What Programmers think of first is:
Const
Constant. This is not a good conditioned reflection. If you only know how to use
Const
Defining constants is equivalent to using gunpowder only to make firecrackers.
Const
The greater charm is that it can modify the parameters, return values, and even the definition body of a function.
Const
Yes
Constant
The abbreviation, "constant. Quilt
Const
Modified things are forcibly protected to prevent unexpected changes and improve program robustness. So many
C ++
Suggestions for programming books:"
Use const whenever you need
".
1.
Use
Const
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 added.
Const
Modify. Otherwise, the output function is lost.
Const
Only input parameters can be modified:
If the input parameter uses "pointer transmission", add
Const
Modification can prevent accidental changes to the pointer and play a protective role.
For example
Stringcopy
Function:
Void stringcopy (char * strdestination, const char * strsource );
Where
Strsource
Is input parameter,
Strdestination
Is the output parameter. To
Strsource
Add
Const
After modification,If the statement in the function body tries to change
Strsource
The compiler will point out the 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
.
For example, do not set the Function
Void func1 (int x)
Write
Void func1 (const int X)
. Similarly, do not set the Function
Void func2 ()
Write
Void func2 (const)
. Where
A
Is the custom data type.
For non-Internal data type parameters, such
Void func ()
The declared functions are destined to be more efficient. Because the function body will generate
A
Temporary objects of the type are used to copy parameters.
A
The construction, replication, and destructor of temporary objects all consume time.
To improve efficiency, you can change the function declaration
Void func (A &)
Because "reference transfer" only uses the parameter alias and does not need to generate a temporary object. But the Function
Void func (A &)
There is a drawback:
"Reference transfer" may change the Parameter
A
This is what we don't expect. It is easy to solve this problem.
Const
So the function becomes
Void func (const A &)
.
And so on, whether
Void func (int x)
Rewrite
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.
The problem is so lingering that I have to
Const &"
Describe the usage of input parameters.
For non-Internal data type input parameters, the "value transfer" method should be changed to"
Const
To improve the efficiency. For example
Void func ()
Change
Void func (const A &)
.
For internal data type input parameters, do not change the "value transfer" method to"
Const
Reference transfer ". Otherwise, the function can not improve the efficiency, but also reduce the comprehensibility of the function. For example
Void func (int x)
Should not be changed
Void func (const Int & X)
.
2. Use const to modify the return value of a function
If you add const 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 returned value can only be assigned to the same type pointer with 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.
3 const member functions
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 functions, 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
It 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.
E. 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.