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
}
解决方案
正如阿里的一句话所说,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
}
但是那个重复的代码,最好使用共享的附加操作来完成这项工作
推荐阅读
- javascript - 日期格式从(20180810)到(2018 年 8 月 10 日)
- c# - 如何从服务响应的 SOAP 标头中删除 UsernameToken 元素
- r - eeptools age_calc() 错误:结束日期必须是出生日期之后的日期
- python - tf.estimator 包未安装
- hive - Mapreduce和hive的区别
- swift - 如何将 Sqlite 中的图像显示到 uicollectionview
- java - Java JAR 占用了高 CPU 使用率和内存消耗
- excel - 仅从 Excel 中的列中选择日期单元格
- c# - “schoolmanamgement.MainForm”不包含带有 7 个参数的构造函数
- qt - Qt::ToolTipRole 没有被触发