移动赋值运算符 C++
Move Assignment operator c++
我不知道我的移动赋值运算符有什么问题,这是函数。我认为我没有正确获取数据,因为当我 运行 测试时,我得到一个随机负数和“你的程序已停止工作)
virtual LinkedList<T> &operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
// Delete our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
delete temp1;
temp1 = n;
}
// Grab other data for ourselves
ListNode<T> *temp2 = other._front;
while (temp2 != nullptr)
{
addElement(temp2->getValue());
temp2 = temp2->getNext();
}
// Reset their pointers to nullptr
other._front = nullptr;
other._end = nullptr;
other._size = 0;
other._last_accessed_index = 0;
other._last_accessed_node = nullptr;
return *this;
}
测试代码-这是我老师的测试代码-
// Use move *assignment* operator
cout << " [x] Test #5: Move *assignment* constructor behavior" << endl;
moved1 = LinkedList<int>{ 6, 7, 8, 9, 10 };
cout << " [x] Result:" << endl;
cout << " [x] Expected:\t6 7 8 9 10" << endl;
cout << " [x] Actual:\t\t";
for (int i = 0; i < moved1.getSize(); i++)
{
cout << moved1.getElementAt(i) << " ";
}
cout << endl << endl;
这是我第一次使用移动和移动赋值运算符。谢谢:)
如果没有其余代码,很难确定,但看起来您没有正确清除正在分配的列表。
当你这样做时:
// Delete our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
delete temp1;
temp1 = n;
}
您实际上并没有从 this
中删除元素。因此,moved1
包含已删除的节点,并且当您开始循环它们时执行失败。您要做的是在删除之前从列表中删除节点。
要走的路是:
// Remove our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
pop();
delete temp1;
temp1 = n;
}
并且有一个像这样的方法:
void pop() // removes the first element from the list
{
_front = _end._front;
_end = _end._end;
--_size;
}
当然 pop
的定义将取决于您对 class 的完整实施。如果你正在存储指向给你的对象的指针,你可能不应该删除它们。但是,如果您正在使用额外的包装器,例如 ListNode
,您的 pop
方法应该删除它们 - 尽管在包装器的情况下,最好不要使用指针。
您可以查看 std::list::pop_front 了解更多信息。
这不是移动赋值运算符的正确实现。它看起来更像是一个复制赋值运算符(但不是一个好运算符,因为它会泄漏内存)。
典型的移动赋值运算符看起来更像这样:
#include <utility>
LinkedList<T>& operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
std::swap(_front, other._front);
std::swap(_end, other._end);
std::swap(_size, other._size);
std::swap(_last_accessed_index, other._last_accessed_index);
std::swap(_last_accessed_node, other._last_accessed_node);
return *this;
}
移动赋值运算符不应释放任何东西。 将源内容的所有权 移动到目标对象,反之亦然。在赋值运算符退出后销毁源对象时,让源对象释放目标对象之前的内容,因此请确保 class 也有适当的析构函数实现:
~LinkedList()
{
// Delete our elements
ListNode<T> *temp = _front;
while (temp != nullptr)
{
ListNode<T> *n = temp->getNext();
delete temp;
temp = n;
}
}
为了更好地衡量,复制构造函数、移动构造函数和复制赋值运算符可能如下所示:
LinkedList() :
_front(nullptr),
_end(nullptr),
_size(0),
_last_accessed_index(0),
_last_accessed_node(nullptr)
{
cout << " [x] Default *constructor* called. " << endl;
}
LinkedList(const LinkedList<T> &src)
: LinkedList()
{
cout << " [x] Copy *constructor* called. " << endl;
ListNode<T> *temp = src._front;
while (temp != nullptr)
{
addElement(temp->getValue());
temp = temp->getNext();
}
}
LinkedList(LinkedList<T> &&src)
: LinkedList()
{
cout << " [x] Move *constructor* called. " << endl;
src.swap(*this);
}
LinkedList(initializer_list<T> src)
: LinkedList()
{
cout << " [x] Initialization *constructor* called. " << endl;
const T *temp = src.begin();
while (temp != src.end())
{
addElement(*temp);
++temp;
}
}
LinkedList<T>& operator=(const LinkedList<T> &other)
{
cout << " [x] Copy *assignment* operator called. " << endl;
if (&other != this)
LinkedList<T>(other).swap(*this);
return *this;
}
LinkedList<T>& operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
other.swap(*this);
return *this;
}
void swap(LinkedList<T> &other)
{
std::swap(_front, other._front);
std::swap(_end, other._end);
std::swap(_size, other._size);
std::swap(_last_accessed_index, other._last_accessed_index);
std::swap(_last_accessed_node, other._last_accessed_node);
}
复制和移动赋值运算符实际上可以合并为一个实现,方法是按值获取输入对象,让编译器根据上下文在初始化该对象时决定是使用复制还是移动语义运营商被称为:
LinkedList<T>& operator=(LinkedList<T> other)
{
cout << " [x] *assignment* operator called. " << endl;
swap(other);
return *this;
}
我不知道我的移动赋值运算符有什么问题,这是函数。我认为我没有正确获取数据,因为当我 运行 测试时,我得到一个随机负数和“你的程序已停止工作)
virtual LinkedList<T> &operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
// Delete our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
delete temp1;
temp1 = n;
}
// Grab other data for ourselves
ListNode<T> *temp2 = other._front;
while (temp2 != nullptr)
{
addElement(temp2->getValue());
temp2 = temp2->getNext();
}
// Reset their pointers to nullptr
other._front = nullptr;
other._end = nullptr;
other._size = 0;
other._last_accessed_index = 0;
other._last_accessed_node = nullptr;
return *this;
}
测试代码-这是我老师的测试代码-
// Use move *assignment* operator
cout << " [x] Test #5: Move *assignment* constructor behavior" << endl;
moved1 = LinkedList<int>{ 6, 7, 8, 9, 10 };
cout << " [x] Result:" << endl;
cout << " [x] Expected:\t6 7 8 9 10" << endl;
cout << " [x] Actual:\t\t";
for (int i = 0; i < moved1.getSize(); i++)
{
cout << moved1.getElementAt(i) << " ";
}
cout << endl << endl;
这是我第一次使用移动和移动赋值运算符。谢谢:)
如果没有其余代码,很难确定,但看起来您没有正确清除正在分配的列表。
当你这样做时:
// Delete our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
delete temp1;
temp1 = n;
}
您实际上并没有从 this
中删除元素。因此,moved1
包含已删除的节点,并且当您开始循环它们时执行失败。您要做的是在删除之前从列表中删除节点。
要走的路是:
// Remove our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
pop();
delete temp1;
temp1 = n;
}
并且有一个像这样的方法:
void pop() // removes the first element from the list
{
_front = _end._front;
_end = _end._end;
--_size;
}
当然 pop
的定义将取决于您对 class 的完整实施。如果你正在存储指向给你的对象的指针,你可能不应该删除它们。但是,如果您正在使用额外的包装器,例如 ListNode
,您的 pop
方法应该删除它们 - 尽管在包装器的情况下,最好不要使用指针。
您可以查看 std::list::pop_front 了解更多信息。
这不是移动赋值运算符的正确实现。它看起来更像是一个复制赋值运算符(但不是一个好运算符,因为它会泄漏内存)。
典型的移动赋值运算符看起来更像这样:
#include <utility>
LinkedList<T>& operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
std::swap(_front, other._front);
std::swap(_end, other._end);
std::swap(_size, other._size);
std::swap(_last_accessed_index, other._last_accessed_index);
std::swap(_last_accessed_node, other._last_accessed_node);
return *this;
}
移动赋值运算符不应释放任何东西。 将源内容的所有权 移动到目标对象,反之亦然。在赋值运算符退出后销毁源对象时,让源对象释放目标对象之前的内容,因此请确保 class 也有适当的析构函数实现:
~LinkedList()
{
// Delete our elements
ListNode<T> *temp = _front;
while (temp != nullptr)
{
ListNode<T> *n = temp->getNext();
delete temp;
temp = n;
}
}
为了更好地衡量,复制构造函数、移动构造函数和复制赋值运算符可能如下所示:
LinkedList() :
_front(nullptr),
_end(nullptr),
_size(0),
_last_accessed_index(0),
_last_accessed_node(nullptr)
{
cout << " [x] Default *constructor* called. " << endl;
}
LinkedList(const LinkedList<T> &src)
: LinkedList()
{
cout << " [x] Copy *constructor* called. " << endl;
ListNode<T> *temp = src._front;
while (temp != nullptr)
{
addElement(temp->getValue());
temp = temp->getNext();
}
}
LinkedList(LinkedList<T> &&src)
: LinkedList()
{
cout << " [x] Move *constructor* called. " << endl;
src.swap(*this);
}
LinkedList(initializer_list<T> src)
: LinkedList()
{
cout << " [x] Initialization *constructor* called. " << endl;
const T *temp = src.begin();
while (temp != src.end())
{
addElement(*temp);
++temp;
}
}
LinkedList<T>& operator=(const LinkedList<T> &other)
{
cout << " [x] Copy *assignment* operator called. " << endl;
if (&other != this)
LinkedList<T>(other).swap(*this);
return *this;
}
LinkedList<T>& operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
other.swap(*this);
return *this;
}
void swap(LinkedList<T> &other)
{
std::swap(_front, other._front);
std::swap(_end, other._end);
std::swap(_size, other._size);
std::swap(_last_accessed_index, other._last_accessed_index);
std::swap(_last_accessed_node, other._last_accessed_node);
}
复制和移动赋值运算符实际上可以合并为一个实现,方法是按值获取输入对象,让编译器根据上下文在初始化该对象时决定是使用复制还是移动语义运营商被称为:
LinkedList<T>& operator=(LinkedList<T> other)
{
cout << " [x] *assignment* operator called. " << endl;
swap(other);
return *this;
}