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



我正在使用一个链接的袋子,需要使用超载操作员创建袋子的副本。当前的代码给我一个空白的袋子。它应该用文本文件返回我放入的值。问题是我试图超载的操作员。

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
}

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

但是那重复的代码,最好使用共享的其他操作来完成作业

最新更新