A very good analysis of the summary of the const, thank you, the original author : http://blog.csdn.net/zcf1002797280/article/details/7816977
In a normal non-const member function, the type of this is a const pointer to the class type. You can change the value that this is pointing to, but you cannot change the address saved by this.
In the const member function, the type of this is a const pointer to the Const class type object. You can neither change the object that this is pointing to nor change the address saved by this.
Keyword: const,const function, const variable, const after function
Seeing the const keyword, the first thing a C + + programmer might think of is a const constant. This is not a good reflex. If you only know to define constants with const, then it is equivalent to using gunpowder only to make firecrackers. The greater charm of Const is that it can modify the parameters of a function, the return value, or even the definition of a function.
Const is the abbreviation of constant, the meaning of "constant unchanged". The const-modified items are protected against accidental changes and can improve the robustness of the program. So many C + + programming books suggest: "Useconst whenever you need".
1. Parameters with the const modifier function
If the parameter is used for output, no matter what data type it is, or whether it takes "pointer passing" or "reference passing", it cannot be const-decorated, otherwise the parameter will lose output function. Const can only modify input parameters:
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.
For example, the Stringcopy function:
void Stringcopy (char*strdestination, const char *strsource);
where strsource is the input parameter, strdestination is the output parameter. After adding a const modifier to strsource, the compiler will indicate an error if the statement in the function body attempts to alter the contents of the strsource.
if the input parameter takes "value pass", since the function will automatically generate a temporary variable for copying the parameter, the input parameter is not intended to be protected, so do not add a const modifier.
For example, do not write the function voidFunc1 (int x) as VOIDFUNC1 (const int x). In the same vein, do not write function VoidFunc2 (a a) as VOIDFUNC2 (const a a). Where a is a user-defined data type.
for parameters of non-intrinsic data types, functions declared like Voidfunc (a a) are doomed to be more efficient than the bottom. Because the body of a function will produce temporary objects of type A for copying parameter A, the construction, copying, and destruction of temporary objects will consume time.
to improve efficiency, you can change the function declaration to Voidfunc (A &a), because "reference passing" only borrows the alias of the parameter and does not need to produce a temporary object. However, the function Voidfunc (a &a) has a disadvantage:
"Reference passing" has the potential to change the parameter a, which we do not expect. It is easy to solve this problem, with the const modifier, so the function eventually becomes Voidfunc (const A &a).
and so on, should voidfunc (int x) be rewritten as Voidfunc (const INT&X) to improve efficiency? It is completely unnecessary, because the parameters of the internal data type do not have the process of construction, destruction, and replication is very fast, and the efficiency of "value passing" and "reference passing" is almost equal.
The problem was so lingering that I had to summarize the usage of the "const&" modifier input parameter.
for input parameters of non-intrinsic data types, the "value passing" method should be changed to "const reference passing" in order to improve efficiency. For example, change Voidfunc (a a) to Voidfunc (const a &a).
for input parameters of an internal data type, do not change the "value passing" method to "Const reference pass". Otherwise, it can not achieve the purpose of improving efficiency, but also reduces the comprehensible function. For example, voidfunc (int x) should not be changed to Voidfunc (const int &x).
2 return value with const modifier function
The contents of a function return value (that is, the pointer) cannot be modified if the return value of the function returned by the "pointer Pass" method is added to the const modifier, and the return value can only be assigned to a const-decorated pointer of the same type. such as functions
const char * GetString (void);
A compilation error will appear in the following statement:
char* str = GetString ();
the correct usage is
const char *STR =getstring ();
if the function return value is in "value passing", the const modifier has no value because the function copies the return value into an external temporary storage unit.
For example, do not write the function intgetint (void) as Constint GetInt (void).
Similarly, do not write function Ageta (void) as Consta geta (void), where a is a user-defined data type.
if the return value is not an internal data type, overwriting the function ageta (void) to Consta &geta (void) can indeed improve efficiency. But be careful at this point, be sure to figure out whether the function is going to return a copy of an object or just return an alias, or the program will go wrong.
There are not many occasions when the return value of a function is used as a "reference pass", which is usually only present in the assignment function of the class, in order to realize the chain expression.
For example:
ClassA
{
A & operate = (const A &other);//Assignment function
};
Aa, B, C;//A, B, C is an object of a
A= B = C; Normal chained assignment
(a= b) = C; Abnormal chained assignment, but legal
If you add a const modifier to the return value of an assignment function, the contents of the return value are not allowed to be altered. In the above example, the statement a= B = c is still correct, but the statement (a= b) = c is illegal.
3 Const member functions
any function that does not modify a data member should be declared as a const type. If you inadvertently modify the data member when you write the const member function, or if you call other non-const member functions, the compiler will indicate the error, which will undoubtedly improve the robustness of the program. In the following program, the member function GetCount of a class stack is used only for counting, and logically getcount should be a const function. The compiler will indicate an error in the GetCount function.
Classstack
{
Public :
void Push (int elem);
int Pop (void);
intgetcount (void) const;//const member function
Private:
Intm_num;
int m_data[100];
};
int Stack::getcount (void) const
{
+ + M_num;//Compile error, attempt to modify data member M_num
Pop ();//Compile error, attempt to invoke non-const function
Returnm_num;
}
The declaration of the const member function looks strange: The const keyword can only be placed at the end of a function declaration, presumably because other places are already occupied.
some rules about the const function:
the A.const object can access only the const member function, and not the Const object to access any member function, including the const member function.
The members of a B.const object cannot be modified, but objects maintained by a const object through pointers can be modified.
the C.const member function cannot modify the object's data, regardless of whether the object is of a const nature. It is checked at compile time, based on whether to modify member data.
E. However, a data member with the mutable modifier can be modified by any means in any case, and the const member function at this time can modify its
Add:
title: What does the const put in the back mean?
--------------------------------------------------------------------------------
a function
acgepoint3dstartpoint () const;
is there a difference between the const and the front?
==>
To be exact, the const is the object that modifies this point .
For example, we define the
classa{
Public :
f (int);
};
here the F function actually has two parameters, the first one is A*const this, the other is the int type parameter
If we do not want the F function to change the value of the parameter, we may change the function prototype to F (constint), but if we do not allow F to change the object of this point? Because this is an implicit parameter, const cannot directly modify it, it is added to the back of the function, indicating that the type of this is Consta *constthis.
The const modifier *this is essentially, as to say "means that the member function does not modify the data of the class." Otherwise it will compile the error "such a statement is only a phenomenon, the root cause is because *this is a const type of
----------------------------------------------------- ---------------------------