使用重载运算符添加构造函数

时间:2019-02-24 19:32:54

标签: c++

我正在处理链接的袋子,需要使用重载运算符创建袋子的副本。当前代码给了我一个空白的袋子。它应该返回我在文本文件中输入的值。问题出在我要重载的运算符上。

template<class ItemType>
LinkedBag<ItemType>::LinkedBag(const LinkedBag<ItemType>& aBag)
{
    itemCount = aBag.itemCount;
    Node<ItemType> *origChainPtr = aBag.headPtr;

    if (origChainPtr == nullptr)
    {
        headPtr = nullptr; // original bag is empty; so is copy
    } else
    {
        // copy first node
        headPtr = new Node<ItemType>();
        headPtr->setItem(origChainPtr->getItem());
        headPtr->setCount(origChainPtr->getCount());

        // copy remaining nodes
        Node<ItemType> *newChainPtr = headPtr;
        origChainPtr = origChainPtr->getNext();
        while (origChainPtr != nullptr)
        {
            // get next node values from original chain
            ItemType nextItem = origChainPtr->getItem();
            int      nextCount = origChainPtr->getCount();

            // create a new node containing the next 2D point
            Node<ItemType> *newNodePtr = new Node<ItemType>(nextItem, nextCount);

            // link new node to end of new chain
            newChainPtr->setNext(newNodePtr);

            // advance pointers
            newChainPtr = newChainPtr->getNext();
            origChainPtr = origChainPtr->getNext();
        }
        newChainPtr->setNext(nullptr);
    }
} // end copy constructor

这是我试图创建的重载运算符 将创建Linkedbag的深层副本。

template<class ItemType>
LinkedBag<ItemType>& LinkedBag<ItemType>::operator=(const LinkedBag<ItemType>& rhs)
{
    if (this != &rhs)
    {
        this->clear(); // Deallocate left-hand side
        //copyBagNode(rhs); // Copy list nodes
        Node<ItemType> *origChainPtr = rhs.headPtr;
        itemCount = rhs.itemCount; // Copy size of list
    } // end if

 // end operator=
    return *this;   // by convention, operator= should return *this
}

1 个答案:

答案 0 :(得分:0)

如Ali所说,rhs.headPtr未复制到this->headPtr中,可能类似于复制构造函数定义中的内容

template<class ItemType>
LinkedBag<ItemType>& LinkedBag<ItemType>::operator=(const LinkedBag<ItemType>& rhs)
{
    if (this != &rhs)
    {
        this->clear(); // Deallocate left-hand side

        itemCount = rsh.itemCount;
        Node<ItemType> *origChainPtr = rsh.headPtr;

        if (origChainPtr == nullptr)
        {
          headPtr = nullptr; // original bag is empty; so is copy
        } else
        {
          // copy first node
          headPtr = new Node<ItemType>();
          headPtr->setItem(origChainPtr->getItem());
          headPtr->setCount(origChainPtr->getCount());

          // copy remaining nodes
          Node<ItemType> *newChainPtr = headPtr;
          origChainPtr = origChainPtr->getNext();
          while (origChainPtr != nullptr)
          {
            // get next node values from original chain
            ItemType nextItem = origChainPtr->getItem();
            int      nextCount = origChainPtr->getCount();

            // create a new node containing the next 2D point
            Node<ItemType> *newNodePtr = new Node<ItemType>(nextItem, nextCount);

            // link new node to end of new chain
            newChainPtr->setNext(newNodePtr);

            // advance pointers
            newChainPtr = newChainPtr->getNext();
            origChainPtr = origChainPtr->getNext();
          }
          newChainPtr->setNext(nullptr);
        }
    }    
 // end operator=
    return *this;   // by convention, operator= should return *this
}

但是有重复的代码,最好使用共享的附加操作来完成工作