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+也可以執行,但需要類型轉換
//優先執行第一種