![](/img/trans.png)
[英]C++ Initializer list when dependencies do not have copy ctor or assignment operator
[英]C++ List is not copying via copy ctor or assignment operator
我有一個程序,手動放入一個列表,它只是一堆名字扔進去: list.PushBack("Frank");
或list.PushFront("George");
我當前的問題是,當我嘗試通過我的復制構造函數和賦值運算符運行已經創建的列表時,列表出現“”(在isEmpty函數中定義)
忽略所有“TODO”評論點,這些都是我自己的心理記錄。
還有一些我用於調試目的的隨機cout語句,並且希望保持一致,直到一切正常。
這是相關代碼:
class Node;
class List
{
public:
List();
List(const char* p);
//Copy constructor
List(const List& str);
//Deep Copy
List& operator=(const List& str);
~List();
void Clear();
//Adds to the front
void PushFront(std::string data);
//adds to the back
void PushBack(std::string data);
//removes from the front
void PopFront();
//removes from the back
void PopBack();
//gets the back value
std::string& Back() const;
//gets the from value
std::string& Front() const;
bool Empty() const {return m_pFront == 0;}
ostream& OutPut(ostream& os);
private:
Node* m_pFront;
Node* m_pBack;
char* m_pData;
};
List::List() : m_pFront(0), m_pBack(0), m_pData(0)
{}
List::~List()
{
Clear();
}
void List::Clear()
{
//delete
if(!m_pFront)
{
return;
}
delete m_pFront;
delete m_pData;
m_pFront = 0;
m_pBack = 0;
}
void List::PushFront(std::string data)
{
//create a new node
Node* p = new Node(data);
//Empty list
if(!m_pFront)
{
m_pFront = p;
m_pBack = p;
}
else //Not empty list
{
p -> m_pNext = m_pFront;
m_pFront -> m_pPrev = p;
m_pFront = p;
}
}
void List::PushBack(std::string data)
{
Node* p = new Node(data);
if(!m_pBack)
{
m_pFront = p;
m_pBack = p;
}
else
{
p -> m_pPrev = m_pBack;
m_pBack -> m_pNext = p;
m_pBack = p;
}
}
void List::PopFront()
{
if(m_pFront == 0)
{
//TODO: we need to handle this problem
return;
}
if(m_pBack == m_pFront)
{
Clear();
return;
}
Node* p = m_pFront;
m_pFront = m_pFront -> m_pNext;
p -> m_pNext = 0;
m_pFront -> m_pPrev = 0;
delete p;
}
void List::PopBack()
{
if(m_pBack == 0)
{
//TODO: we need to handle this problem
return;
}
if(m_pBack == m_pFront)
{
Clear();
return;
}
Node* p = m_pBack;
m_pBack = m_pBack -> m_pPrev;
p -> m_pPrev = 0;
m_pBack -> m_pNext = 0;
delete p;
}
ostream& List::OutPut(ostream& os)
{
if(Empty() == true)
{
os << "<empty>";
}
else
{
m_pFront -> OutputNode(os);
}
return os;
}
std::string& List::Back() const
{
if(m_pBack == 0)
{
//TODO: we need to handle this problem
}
return m_pBack -> GetData();
}
std::string& List::Front() const
{
if(m_pFront == 0)
{
//TODO: we need to handle this problem
}
return m_pFront -> GetData();
}
//Copy Constructor
List::List(const List& str)
{
if(str.m_pData)
{
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
}
else
{
m_pData = 0;
}
}
//Deep copy
List& List::operator=(const List& str)
{
//Check for self assignment
if(this == &str)
{
return *this;
}
//Deallocate any value the string is holding
delete [] m_pData;
//Now we need to deep copy m_pData
if(str.m_pData)
{
//Allocate memory for the copy
m_pData = new char[strlen(str.m_pData) + 1];
//Copy the parameter to the newly allocated memory
strcpy(m_pData, str.m_pData);
}
else
{
m_pData = 0;
}
return *this;
}
你可以改變char* m_pData;
to std::string m_sData
,否則,需要手動為m_pData分配內存。
和m_pData = str.m_pData;
會導致問題,因為你讓m_pData指向str.m_pData,如果str.m_pData被刪除了它。 m_pData指向未知的地方。
在你的拷貝構造函數中
//Copy Constructor
List::List(const List& str)
{
if(Empty() == true)
{
m_pData = 0;
return;
}
m_pData = str.m_pData;
strcpy(m_pData, str.m_pData);
}
你使用的是Empty() bool Empty() const {return m_pFront == 0;}
並且到目前為止還沒有初始化m_pFront。
並且m_pData = str.m_pData
不需要strcpy。
而是復制此字符串(首先分配然后復制)或使用std :: string。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.