Reputation: 1
std::ostream &operator <<(std::ostream &o, date &d)
{
return o << d.year << d.month << d.day;
}
std::ostream &operator <<(std::ostream &o, date &d)
{
o << d.year << d.month << d.day;
return o;
}
Which of these is correct syntax or are they both correct and interchangable? Also,in which all cases we need curly braces,any rule for that,its too confusing.
Upvotes: 0
Views: 56
Reputation: 5201
Some comments.
First of all, I encourage you to keep std::
before the objects of the standart library. This avoids a lot of name conflicts problems.
For me, the second version is better, due to the fact that operator <<
can be overloaded by other users, and sometimes, it's not well written, and the return is missing. Then, the first syntax will fail.
Moreover, in general, dump an object in a stream doesn't modify it. Then, pass the object const.
Then, in my opinion, the best is the following :
std::ostream& operator << (std::ostream& out, const Date& date)
{
out << date.year << "-" << date.month << "-" << date.day;
return out;
}
Upvotes: 1