我正在编写自己的字符串类,只是为了学习和巩固一些知识。除了我想要使用带有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;
}