ostringstream和复制构造函数的问题

ostringstream和复制构造函数的问题

本文介绍了ostringstream和复制构造函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译类T失败,原因是Visual C ++和GCC产生iostreams模板错误。以下是代码:

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;

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

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

推荐答案

已经对C ++中的任何流类进行了复制构造函数和复制分配私人。这意味着,您无法复制 std :: ostringstream 对象:

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

这篇关于ostringstream和复制构造函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:18