why does
void operator<<(ostream out, Test &t);
return an error whereas
void operator<<(ostream &out, Test &t);
does not ?
views:
93answers:
1
+11
A:
Because you cannot copy streams, you have to pass them per reference.
Note that the canonical form of operator<<
is this:
std::ostream& operator<<(std::ostream& out, const Test &t)
{
// write t into out
return out;
}
returning the stream is important so that you can string output together:
std::cout << Test() << '\n';
sbi
2010-04-20 10:46:02
visitor
2010-04-20 11:36:01
@visitor: You're certainly right, I copied without looking closely enough. Thanks for catching this!
sbi
2010-04-20 11:40:44