function Signature: The name of the function and its parameter types are combined to define a unique attribute called a function signature. (not including return type)
When you write a statement that contains a function call, the compiler uses the call to create a function signature. Compare it to the function signature set available in the function prototype/or definition. If a matching function name is found, the called function is established.
overloading: Multiple identical function names, the number of arguments not allowed, or the form of a type is called an overload of the function.
Attention: overloading and referencing parameters
#include <iostream>
Double larger (double a,double b);
long& larger (long& a,long& b);
using namespace Std;
void Main ()
{
cout<< "lager of 1.5 and 2.5 is" <<larger (1.5,2.5) <<endl;
int value1=35;
int value2=45;
cout<< "Lager of" <<static_cast<long>value1<< "and" <<static_cast<long>value2 << "is" <<larger (value1,value2) <<endl;
GetChar ();
}
Double larger (double a,double b)
{
cout<< "double version:";
Return a>b?a:b;
}
long& larger (long& a,long& b)
{
cout<< "Long ref version:";
Return a>b?a:b;
}
Originally the second static_cast<long> after the output is expected to be "long ref version", why is this result?
The arguments are not value1 and value2, but are temporary locations that contain the same values, which are converted to a long type. Behind the scenes, the compiler is not prepared to use temporary addresses to eat petrified references, which is too risky. The code in larger () is free to control what it does with reference parameters, and in theory, two reference parameters can be modified and returned. Because it is not advisable to use a temporary location in this way, the compiler does not use it.
How do I deal with this problem?
1 You can declare value1 value2 as a long type
2 If the environment does not allow this, the reference parameter can also be declared as const:long& larger (const long& a,const long& B). Be sure to modify both the function prototype and the definition at the same time, informing the compiler that the parameter cannot be modified, so the compiler allows the version to be called instead of the version with the parameter double.
function signatures and overloads