overloaded Function in c++

来源:互联网 发布:华为网络营销策划书 编辑:程序博客网 时间:2024/05/17 00:53

The overloaded function is an important concept in C++.

What's the definition?

Function that have the same name but different parameter lists and that appear in the same scope are overloaded. Generally speaking,  the function have the same name with different parameter list, but declaration must besame-type in the program.

C++ primer give an example:

Record lookup (const  Account&)

Record lookup (const Phone&)

Record lookup(const Name&)

all three functions share the same name, yet they are three distinct functions. The compiler uses the argument type(s) to figure out which function to call.

Overloaded functions must differ in the number or the type(s) of their parameters. Each of the functions above takes a single parameter, but the parameters have different types.

It's an error for two functions to differ only in terms of their return types. If the parameter lists of two functions match but the return types differ.

bool lookup(const Account&) // error,  behavior is incorrect.

Compiler deduce  which it would be call? So, we need to know how the parameter type are identified?

for example:

 Record lookup (const Account &acct)

Record lookup (const Account &)

the two are the same declaration. the next we get is the same answer

typedef Phone Telno

Record lookup (const Phone&)

Record lookup (const Telno&)

the parameter  looks like the type are different, but Telno is not a new type. It's a synonym for phone.


Quoted from <<C++ primer>>

Calling an OVerloaded Function

Once we have defined a set of overloaded functions, we need to be able to call themm with appropriate arguments. Function mathcing (also known as overload resolution) is the process by which a particular function call is associated with a specific function from a set of overloaded functions. The compiler determines which function to call by comparing the arguments in the call with the parameters offered by each function in the overload set.

In many -probably most-cases, it's straightforward for a programmer to determine whether a particular call is legal and , if so, which function will be called. Often the functions in the overload set differ in terms of the number of arguments, or the types of the arguments are unrelated. In usch cases, it's easy to determine which function is called. Determining which function is called when the overloaded functions have the same number of parameters and those parameters are related by conversions can be less obvious. We'll look at how the compiler resolves calls involving conversions..

For now, what's important to realize is that for any given call to an overloaded function, there are three possible outcomes:

*) The compiler finds exactly one function that is a best match for the actual arguments and generates code to call that function.

*) There is no function with parameters that match the arguments in the call, in which case the compiler issues an error message that there was no match.

*) There is more than one function taht matches and none of the matches is clearly best. This case is also an error. it's an ambiguous call.

(TBD)

0 0
原创粉丝点击