开发者

Stream overloading in C++

开发者 https://www.devze.com 2022-12-28 00:43 出处:网络
why does void operator<<(ostream out, Test &t); return an error wherea开发者_Go百科s void operator<<(ostream &out, Test &t);

why does void operator<<(ostream out, Test &t); return an error wherea开发者_Go百科s void operator<<(ostream &out, Test &t); does not ?


Because you cannot copy streams, you have to pass them per reference.

Note that the canonical form of operator<< is this:

std::ostream& operator<<(std::ostream& out, const Test &t)
{
   // write t into out
   return out;
}

returning the stream is important so that you can string output together:

std::cout << Test() << '\n';
0

精彩评论

暂无评论...
验证码 换一张
取 消