![](/img/trans.png)
[英]"Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++
[英]Overloading the << operator in C++ -why return type is a reference std::ostream&
我正在閱讀有關在C ++中重載<<
運算符的教程 。
我試圖理解為什么返回類型是引用std::ostream&
? 本教程說
如果嘗試按值返回std :: ostream,則會收到編譯器錯誤。 發生這種情況是因為std :: ostream明確禁止復制。
有人可以解釋std::ostream
被復制了嗎?
std::ostream& operator<< (std::ostream &out, const Point &point)
{
// Since operator<< is a friend of the Point class, we can access Point's members directly.
out << "Point(" << point.m_x << ", " << point.m_y << ", " << point.m_z << ")";
return out;
}
在C ++中,當您執行以下操作時,將復制對象:
在第三種情況下,如果返回類型為std::ostream
則將復制'out'。 所以應該是std::ostream&
。 (您可能會注意到,第一個參數的類型也是std::ostream&
,避免復制)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.