我正在編寫自己的字符串類,僅僅是爲了學習和鞏固一些知識。我有一切工作,除了我想有一個使用移動語義與std :: string的構造函數。試圖編寫可以從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;
}
相關問題:http://stackoverflow.com/questions/9957840/move-string-into-vector – nobar 2012-09-22 22:54:02