Wednesday, 11 September 2013

Does cout object remain a single instance, i.e. it never gets copied?

Does cout object remain a single instance, i.e. it never gets copied?

Is cout ever copied implicitly?
For example, is the cout object passed to the second overloaded operator
in the code below, and the cout object inside its implementation are the
same objects or is one a copy of cout?
My understanding is that the first implementation is correct because the
<< operator works for any ostream object, e.g. it will work for ofstream
objects for writing to files.
//First implementation
ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}
//Second implementation
ostream& operator<<(ostream& os, const Date& dt)
{
cout << dt.mo << '/' << dt.da << '/' << dt.yr;
return cout;
}
//using second implementation on object date
cout<<date;

No comments:

Post a Comment