function signatures and overloads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.