I'm a student and quite new to c++. I created a Class Vector2D and overloaded the istream operator. I did it in the following 2 ways:
friend void operator >> (istream& is, Vector& v);
friend istream& operator >>(istream& is, Vector&v);
void operator >> (istream& is, Vector& v)
{
is >> v.x >> v.y;
}
istream& operator >> (istream& is, Vector& v)
{
is >> v.x >> v.y;
return is;
}
Both ways did the same thing (for me), and I don't know why I should return a istream reference. I mean, I don't care about the istream object, I just need it to initialize my objects. What's the reason for returning it?
Because streaming operator can be chained, i.e.,
std::cin >> a >> b;
Without returning a reference, this is not possible.