[英]How do I write a copy constructor for my class which has a std::stringstream member?
If I have a class like this, how should I write the copy constructor? 如果我有这样的类,我该如何编写复制构造函数?
#include <stringstream>
class MyClass {
std::stringstream strm;
public:
MyClass(const MyClass& other){
//...
}
std::string toString() const { return strm.str(); }
};
std::stringstream has no copy constructor itself, so I can't use an initialiser list like this: std :: stringstream本身没有复制构造函数,所以我不能使用这样的初始化列表:
MyClass(const MyClass& other): strm(other.strm) {}
你可以试试这个:
MyClass(const MyClass& other): strm(other.strm.str()) {}
If your compiler does not support C++0x, or not want to use move constructor : 如果你的编译器不支持C ++ 0x,或者不想使用move构造函数 :
MyClass(const MyClass& other)
: strm(other.strm.str())
{
this->strm.seekg( other.strm.tellg() );
this->strm.seekp( other.strm.tellp() );
this->strm.setstate( other.strm.rdstate() );
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.