Non-const Ostream Object Overloading <<
Why can't os be const when overloading the << operator? You don't want to change os, right?
1 2 3 4 5 6 7 8 9 10 11 12 13
|
ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}
instead of
ostream& operator<<(const ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}
|
You change the state of os when you write to it.
Peter87 said it all
Topic archived. No new replies allowed.