Function overloading in C++

Function overloading, which seems to be a simple topic, is in fact much more complicated than we expect. An excellent book <C++ Primer> (I read the third edition) provides a full cover on it.

In short, there are two cases of function overloading, the normal function overloading in a certain scope and the member function overloading in a class scope. The compiler will follow three steps to select a correct form of a particular function call.

First of all, the compiler consider all the functions with the same name of that function call and visible at the calling point as the candidate functions. Meantime, figure out the number and the types of the real parameters of that call. In this step, the meanings of visible are different between overloading in a certain scope and in a class scope. Secondly, compare the number and the types with every candidate function's parameter list, to get the viable functions for that call. Any candidate function whose parameter list matches the real parameter list, which means that the real parameters have the same type with the corresponding ones in the parameter list of one function in three cases: accurate match, standard convertion, and user-defined convertion, can be a viable function of that real call. Finally, the best match among the viable functions is selected to finish that call. The rule for best match is that accurate match is better than standard convertion, and standard convertion is better than user-defined convertion.

To get more details about function overloading, I strongly recommend you to turn to <C++ Primer>. The related chapters in the third edition are: chapter 9 (the whole chapter focusing on the normal function overloading), chapter 15 (the whole chapter focusing on the member function overloading and the operator overloading of class), and chapter 19 part 3 (focusing on the overloading problem in the hierarchy of inheritance).

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值