C++ Primer(第5版) 练习 14.52
练习 14.52 在下面的加法表达式中分别选用了哪个operator+?列出候选函数、可行函数及为每个可行函数的实参执行的类型转换:
struct LongDouble{
//用于演示的成员opeartor+;在通常情况下+是个非成员
LongDouble operator+(const SmallInt&);
//其他成员与14.9.2节(第521页)一致
};
LongDouble operator+(LongDouble&, double);
SmallInt si;
LongDouble ld;
ld = si + ld;
ld = ld + si;
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
struct LongDouble{
//用于演示的成员opeartor+;在通常情况下+是个非成员
LongDouble operator+(const SmallInt&);
//其他成员与14.9.2节(第521页)一致
};
LongDouble operator+(LongDouble&, double);
SmallInt si;
LongDouble ld;
ld = si + ld;
//SmallInt转换为int, LongDouble转换为double或float,double或float转换为int,执行int加法
//Smallint转换为int,int转换为double,LongDouble转换为double,执行double加法
//以上执行都会产生二义性
ld = ld + si;
//LongDouble定义的operator+可以执行相加
//SmallInt内的operator+也可以执行,但需要类型转换
//优先执行第一种