开发者

Problem with ostringstream and copy constructor [duplicate]

This question already has answers here: 开发者_JAVA技巧 Closed 11 years ago.

Possible Duplicates:

Why copying stringstream is not allowed?

how copy from one stringstream object to another in C++?

Compiling class T fails with Visual C++ and GCC producing iostreams template errors. Here is the code:

#include <sstream>

class T
{
  static T copy;

  std::ostringstream log;

  T()            {}
  T(const T& t)  {log  = t.log;}
  ~T()           {copy = *this;}
};

T T::copy;

Changing log data member type to string makes it compile and run OK. Is this a legitimate behavior?


Copy constructor and copy-assignment of any stream class in C++ has been made private. That means, you cannot make copy of std::ostringstream object:

std::ostringstream ss;

std::ostringstream ss1(ss); //not allowed - copy-constructor is private
ss1=ss; //not allowed - copy-assignment is private


std::ostringstream is not copyable that's why you are getting error. See this answer for more details to know how you can overcome this problem.

T(const T& t)  {log << t.log.rdbuf(); }


I think ostringstream has no overloaded assignment(=) operator.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜