c++重载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。达到链式的作用。