Using << operator to write to both a file and cout

前端 未结 6 1101
滥情空心
滥情空心 2020-12-02 01:10

I\'d like to overload << operator to write the value it takes to a file and cout. I have tried to do it with following code, but couldn\'t succeed it. It just writes t

6条回答
  •  鱼传尺愫
    2020-12-02 01:25

    It just writes the value to the text file because your operator<< method does not get called.

    The operator<< should return a reference to the stream (ostream&) because otherwise << str << endl; would not work.

    The other problem with your operator<< is that it could create an infinite loop since os << str; has the same signature than fl << "string";

提交回复
热议问题