c++重载ostream的实现

2015-07-20 17:39:45 · 作者: · 浏览: 5
#include 
  
   
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<<" "<
   
主要的注意点就是,要把函数作为class Point的友元函数,在类的外面进行定义,此外要注意return os,达到链式的作用。