gpt4 book ai didi

c++ - move 赋值运算符 C++

转载 作者:太空宇宙 更新时间:2023-11-04 10:16:08 25 4
gpt4 key购买 nike

我无法判断我的 move 赋值运算符有什么问题,这是函数。我认为我没有正确获取数据,因为当我运行测试时,我得到一个随机的负数和“你的程序已停止工作”

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;

这是我第一次使用 move 和 move 赋值运算符。谢谢:)

最佳答案

这不是 move 赋值运算符的正确实现。它看起来更像是一个复制赋值运算符(但不是一个好的运算符,因为它会泄漏内存)。

一个典型的 move 赋值运算符看起来更像这样:

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

move 赋值运算符不应该释放任何东西。将源内容的所有权 move 到目标对象,反之亦然。当赋值运算符退出后销毁源对象时,让源对象释放目标对象之前的内容,因此请确保该类也有适当的析构函数实现:

~LinkedList()
{
// Delete our elements
ListNode<T> *temp = _front;
while (temp != nullptr)
{
ListNode<T> *n = temp->getNext();
delete temp;
temp = n;
}
}

为了更好地衡量,复制构造函数、 move 构造函数和复制赋值运算符可能如下所示:

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

复制和 move 赋值运算符实际上可以合并到一个实现中,通过按值获取输入对象并让编译器在初始化该对象时根据对象所在的上下文决定是使用复制还是 move 语义运营商被称为:

LinkedList<T>& operator=(LinkedList<T> other)
{
cout << " [x] *assignment* operator called. " << endl;
swap(other);
return *this;
}

关于c++ - move 赋值运算符 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com