operator<< overloading ostream

后端 未结 3 1843
离开以前
离开以前 2021-01-18 13:16

In order to use cout as such : std::cout << myObject, why do I have to pass an ostream object? I thought that was an implicit parameter.

ostream &o         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 13:48

    You aren't adding another member function to ostream, since that would require redefining the class. You can't add it to myClass, since the ostream goes first. The only thing you can do is add an overload to an independent function, which is what you're doing in the example.

提交回复
热议问题