C ++ List不通过复制ctor或赋值运算符进行复制

时间:2013-08-29 03:26:33

标签: c++

我有一个程序,手动放入一个列表,只有一堆名字像这样被抛出:list.PushBack("Frank");list.PushFront("George");

我当前的问题是,当我尝试通过我的复制构造函数和赋值运算符运行已经创建的列表时,列表出现了"" (在isEmpty函数中定义)

忽略所有" TODO"评论点,这些都是我自己的心理记录。

还有一些我用于调试目的的随机cout语句,并且希望保持一致,直到一切正常。

以下是相关代码:

List.h

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.cpp

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;
}

2 个答案:

答案 0 :(得分:1)

您可以将char* m_pData;更改为std::string m_sData,否则,您需要手动为m_pData分配内存。

m_pData = str.m_pData;会导致问题,因为你让m_pData指向str.m_pData,如果str.m_pData删除了它。 m_pData指向未知的地方。

答案 1 :(得分:1)

在您的复制构造函数

//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);
}

你正在使用bool Empty() const {return m_pFront == 0;}的Empty(),到目前为止还没有初始化m_pFront。

m_pData = str.m_pData并不需要strcpy。

而是复制此字符串(首先分配然后复制)或改为使用std :: string。