overloaded Function in c++

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值