c++中输出输入运算符的重载

<<和>>运算符重载,均为friend友元函数。//标题竟然不能有 '<' 和 '>' 符号

class Vehicle {
public:
	int i;
	friend ostream& operator<<(ostream &out, const Vehicle &vehicle);//重载<<运算符
};
ostream& operator<<(ostream &out, const Vehicle &vehicle) {//重载<<输出vehicle
	out>>vehicle.i>>endl;
	return out;
}

class Frame {
public:
	int ID, weight;
	Frame() {
		this->ID = 0;
		this->weight = 0;
	}
	friend istream& operator>>(istream &in, const Frame &frame);//Frame的<<
};
istream& operator>>(istream &in, Frame &frame) {//frame的>>重载输入
	in >> frame.ID >> frame.weight;
	if (!in) {
		frame = Frame();
	}
	return in;
}

猜你喜欢

转载自blog.csdn.net/zyw0929/article/details/79905447