Before complaining, let's take a look at the definition of the overload function: two functions that appear in the same scope. If they have the same name but different form parameters, they are called the overload function.
According to dogmatism ,@.@~~ Oh, wrong. According to the definition in the textbook, if there are two functions with the same name, the two parameters have the same name and type, but the parameters of a function are modified with Const, the other function does not carry the const modifier, so these two functions should also be overloaded functions. Like the following two versions:
//
// Version 1
//
Int increase (Int & Val)
{
Return ++ val;
}
//
// Version 2
//
Int increase (const Int & Val)
{
Return Val + 1;
}
Yes, according to ide compilation, they are indeed heavy-duty functions, and in use, version 2 is more loose than Version 1.
When a function like version 1 is called (the parameter is not referenced by const), the real parameter cannot be modified with const, and the real parameter and the type of the form parameter must be consistent. Otherwise, the compiler reports an error. When a function like version 2 is called (the parameter is referenced by the const), if the real parameter type does not match, the function will convert the type as much as possible, create an unnamed temporary variable to store the value of the real parameter and reference the parameter as the temporary variable.
In order to better understand the obscure description above, the following DEMO code is provided:
# Include <cstdlib>
# Include <iostream>
Using STD: cout;
Using STD: Endl;
// Cout <increase (1) <Endl; // error, 1 is not a reference.
// Cout <increase (svalue) <Endl; // error. svalue is not of the int type.
Cout <increase (ivalue) <Endl; // correct, exact type match.
// Cout <increase (dvalue) <Endl; // error. dvalue is not of the int type.
// Cout <increase (svalue) <Endl; // error. svalue cannot be const.
// Cout <increase (ivalue) <Endl; // error. The ivalue cannot be const.
// Cout <increase (dvalue) <Endl; // error. dvalue cannot be const.
Return exit_success;
} Int main (void)
{
Short svalue = 10;
Int ivalue = 20;
Double dvalue = 30;
Const short svalue = 40;
Const int ivalue = 50;
Const double maid = 60.0 ;//
// Version 1
//
Int increase (Int & Val)
{
Return ++ val;
}
In contrast, version 2 is better:
# Include <cstdlib>
# Include <iostream>
Using STD: cout;
Using STD: Endl;
// You can see it! All the code is compiled.
Cout <increase (1) <Endl;
Cout <increase (svalue) <Endl;
Cout <increase (ivalue) <Endl;
Cout <increase (dvalue) <Endl;
Cout <increase (svalue) <Endl;
Cout <increase (ivalue) <Endl;
Cout <increase (dvalue) <Endl;
Return exit_success;
} Int main (void)
{
Short svalue = 10;
Int ivalue = 20;
Double dvalue = 30;
Const short svalue = 40;
Const int ivalue = 50;
Const double maid = 60.0 ;//
// Version 2
//
Int increase (const Int & Val)
{
Return Val + 1;
}
The reason why Version 1 and Version 2 are different is that when the function is called, the actual participation parameter types are different. If the two types can be implicitly converted, an anonymous temporary const object with the same type as the parameter is generated. If the parameter is not a const object, an error occurs.
If the scope is in a class body, you can also include the overloaded Version 3 and the overloaded version 4, that is, the const member function, as shown below:
Class clx
{
Public:
// Version 1
Int increase (Int & Val );
// Version 2
Int increase (const Int & Val );
// Version 4
Int increase (const Int & Val) const;
}; // Version 3
Int increase (Int & Val) const;
Versions 3 and 4 are called by clx objects with Const. The const object can only call the const version method.
On the above page, I intentionally ignored the two versions because they are quite painful, as shown below:
//
// Version
//
Int increase (const int Val)
{
Return Val + 1;
}
//
// Version B
//
Int increase (INT Val)
{
Return ++ val;
}
Why does it hurt ?? Because they seem to be overloaded but not overloaded, they have to be reloaded. The designers of C ++ are also very anxious. No way, this is a legacy problem of history. To be compatible with C, they must not be overloaded. This is life, and sometimes life is so helpless.
In C, there is no difference between version A and Version B. If these two versions are defined in the same scope, the C compiler will consider them as function redefinition rather than function overloading, because the C root has no concept of overloading. Since function parameters in C only have one way to pass values, const only tells the compiler that the nominal value of the form parameters in the function cannot be changed. However, this has nothing to do with the real parameters, but it is only a copy. The difference is that C ++ has a reference, and transferring a reference will affect the real parameter.
PS: writing this article is a tough task: <