Non-const Ostream Object Overloading <<

Jul 27, 2014 at 7:14am
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;
}
Jul 27, 2014 at 7:27am
You change the state of os when you write to it.
Jul 27, 2014 at 7:36am
Peter87 said it all
Topic archived. No new replies allowed.