首页 > 其他 > 详细

重载函数的调用匹配规则

时间:2016-10-12 11:29:49      阅读:348      评论:0      收藏:0      [点我收藏+]

重载函数的调用匹配,依次按照下列规则来判断:

         精确匹配:参数匹配而不做转换,或者只是做微不足道的转换,如数组名到指针、函数名到指向函数的指针、T到const T;

         提升匹配:即整数提升(如bool到int、char到int、short到int),float到double;

         使用标准转换匹配:如int到double、double到int、double到long double、Derived*到Base*、T*到void*、int到unsigned int;

         使用用户自定义匹配;

         使用省略号匹配:类似于printf中省略号参数。

假如运行环境int类型4bytes,short类型2bytes,long类型8bytes,存在代码:
    unsigned short x = 65530;
    int a = myfunc( x, 20.0 );
会优先匹配以下哪一个重载函数?
A. int myfunc( double, double ) 
B. int myfunc( short, double )
C. double myfunc( int, float )
D. double myfunc( int, double)

答案为D!

重载函数的调用匹配规则

原文:http://www.cnblogs.com/bonelee/p/5951718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!