【发布时间】:2012-07-26 17:54:54
【问题描述】:
我正在编写自己的字符串类,实际上只是为了学习和巩固一些知识。除了我想要一个使用带有 std::string 的移动语义的构造函数之外,我一切正常。
在我的构造函数中,我需要复制并清空 std::string 数据指针和其他内容,它需要保持为空但有效的状态,而不删除字符串指向的数据,我该怎么做?
到目前为止我有这个
class String
{
private:
char* mpData;
unsigned int mLength;
public:
String( std::string&& str)
:mpData(nullptr), mLength(0)
{
// need to copy the memory pointer from std::string to this->mpData
// need to null out the std::string memory pointer
//str.clear(); // can't use clear because it deletes the memory
}
~String()
{
delete[] mpData;
mLength = 0;
}
【问题讨论】:
标签: c++ c++11 dynamic-memory-allocation stdstring move-semantics