ostringstream和复制构造函数的问题

时间:2011-08-13 10:00:28

标签: c++ copy-constructor ostringstream

  

可能重复:
  Why copying stringstream is not allowed?
  how copy from one stringstream object to another in C++?

编译类T失败,Visual C ++和GCC生成iostreams模板错误。这是代码:

#include <sstream>

class T
{
  static T copy;

  std::ostringstream log;

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

T T::copy;

log 数据成员类型更改为字符串使其编译并运行正常。这是合法的行为吗?

3 个答案:

答案 0 :(得分:3)

已在C ++中复制构造函数和任何流类的复制赋值private。这意味着,您无法复制std::ostringstream对象:

std::ostringstream ss;

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

答案 1 :(得分:3)

std::ostringstream不可复制,这就是您收到错误的原因。有关详细信息,请参阅this answer以了解如何解决此问题。

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

答案 2 :(得分:1)

我认为 ostringstream 没有重载的赋值(=)运算符。