c++重載ostream的實現,重載ostream實現

來源:互聯網
上載者:User

c++重載ostream的實現,重載ostream實現

#include <iostream>using namespace std;class Point{public:Point(int _x = 0, int _y = 0, int _z = 0):x(_x), y(_y), z(_z){}Point(){}~Point(){}friend ostream& operator<<(ostream &os, const Point &pd);private:int x;int y;int z;};ostream& operator<<(ostream &os, const Point &pd){os << pd.x<<" "<<pd.y<<" "<<pd.z;return os;} int main() {Point pd(1,2,3);cout<<pd;return 0;}
主要的注意點就是,要把函數作為class Point的友元函數,在類的外面進行定義,此外要注意return os,達到鏈式的作用。
C++進 ostream類的重載

ostream有char*的重載啊,
const char*的和const void*的,其他的指標就是按const void*輸出
 
c++中運算子(\" << \")重載ostream&是什

運算子多載,是對現有操作符使用於自訂使用者類型的一種擴充,但具體的性質不能變,如<<表示流操作符左側對象為流的本質不能變,+為兩元操作符本質不能變,再如,對於->重載必須返回指標類型不可變!
即,即使進行運算子多載,也只是有限自由!
friend ostream & operator << (ostream &os, const CPolynomial&)
operator是進行運算子多載的關鍵詞,它後面就是重載的運算子,左側是流,右側是流處理的對象。這樣你就可以處理如何輸入或輸出該對象
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.