重载函数的调用匹配规则

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

         精确匹配:参数匹配而不做转换,或者只是做微不足道的转换,如数组名到指针、函数名到指向函数的指针、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!

posted @ 2016-10-12 10:07  bonelee  阅读(4985)  评论(0编辑  收藏  举报