码迷,mamicode.com
首页 > 编程语言 > 详细

c++重载ostream的实现

时间:2014-09-19 04:29:05      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:c++

#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的实现

标签:c++

原文地址:http://blog.csdn.net/hjiam2/article/details/39390937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!