—1—<<运算符重载C++在输出内容时,最常见的方式:std::cout<<1<<"hello";提问:那为什么这个说法可以成立呢?什么是cout?为什么<<运算符与cout一起工作?原因:cout实际上是iostream头文件中定义的ostream类的一个对象。<<可以用在cout上,因为<<在ostream类中被重载了。对于以下语句:std::cout<<1<<"hello";可以通过以下方式将其重载为ostream类的成员函数:ostream&ostream::operator<<(intn){....//输出n整数代码return*this;}ostream&ostream::operator<<(constchar*s){....//outputsstringcodereturn*this;}那么当使用streaminsertion<
