![](/img/trans.png)
[英]Invalid operands to binary expression error (missing const) in std::sort: Why does specifying the compare operator solve it?
[英]Why does adding “const” solve “invalid operands to binary expression”
我已經通過Integer operator+(Integer & a, Integer & b)
重載了運算Integer operator+(Integer & a, Integer & b)
。 但是當我執行a = b + c + d時 ,它將無效操作數的錯誤賦予二進制表達式 。 但是通過將const
添加到參數中,不會再出現錯誤。 為什么會這樣?
b + c + d
為b + c
的結果生成一個臨時值。 然后,對該臨時變量的引用將傳遞給對operator+
()的第二次調用。
僅const
引用可以綁定到臨時對象。
有關更多討論,請參見非常量引用為什么不能綁定到臨時對象?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.