此C ++赋值运算符代码是否不正确? [英] Is this C++ assignment operator code incorrect?

查看:67
本文介绍了此C ++赋值运算符代码是否不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本书,它包含一个赋值算子的代码,我很确定它是不正确的但我也觉得我可能是错的,因为它这本书不太可能出错。这是代码:

 LinkedList& LinkedList ::  operator  =( const  LinkedList& other)
{
// 确保我们没有分配给自己 - 我们可以忽略
// 如果它发生。请注意,我们在这里使用'this'来确保
// 其他值与对象的地址不同
如果 this == & other)
{
// 返回此对象以保持分配链活着
返回 * ;
}
// 在复制新值之前,我们需要释放旧内存
// 因为它已不再使用
delete _p_head;
_p_head = NULL;
LinkedListNode * p_itr = other._p_head;
while (p_itr!= NULL)
{
insert(p_itr-> val);
}
}



问题是,他从不递增p_itr所以p_itr总是指向列表的头部。代码是不正确的吗?



我还有一个单独的问题,因为这个赋值运算符正在处理对变量的引用,为什么它需要返回任何内容返回值是什么?



最后一个无关的问题是这里有一些代码定义了operator =但是在函数之前没有给出类型:



 operator =(const Player& other); 





这是结构私有部分内的代码。为什么没有类型?



我尝试过:



没什么............................................... ..................................................................................................... 。



复制构造函数签名类似于 LinkedList(const LinkedList& rhs);

记住构造函数不返回任何内容。



引用:

是使用对变量的引用,为什么它需要返回任何内容以及返回值在哪里?



这样你就可以做类似 list1 = list2的事情= list3 = list4 = ...



Quote:

他永远不会增加p_itr



您可能需要查看 insert 方法以确定是否有任何方法发生在那里。



Quote:

operator =(const Player&其他);



不正确。实际上这是复制构造函数的签名,如 Player(const Player& other);


在复制构造函数中,您必须创建a FULL COPY 表示新对象。所以你必须为构造的对象复制所有成员元素。



复制链表时,你必须创建一个副本每个元素并将其列入新名单。



您需要:

播放器&  operator  =( const  Player& other); 





请参阅此操作员教程

I am reading a book and it includes code for an assignment operator and I'm pretty sure it is incorrect but I also think maybe I am just wrong because it is unlikely the book will make a mistake. Here is the code:

LinkedList& LinkedList::operator= (const LinkedList& other)
{
// make sure we aren't assigning to ourself--we can just ignore
// that if it happens. Notice that we're using 'this' here to ensure
// that the other value isn't the same address our as object
if ( this == & other )
{
// return this object to keep the chain of assignments alive
return *this;
}
// before copying over the new values, we need to free the old memory
// since it's no longer used
delete _p_head;
_p_head = NULL;
LinkedListNode *p_itr = other._p_head;
while ( p_itr != NULL )
{
insert( p_itr->val );
}
}


The problem is, he never increments p_itr so p_itr is just always pointing to the head of the list. Is the code incorrect?

Also a separate question I have is, as this assignment operator is working with references to the variables, why does it need to return anything and where are the return values going?

And last question unrelated is here is some code that defines the operator= but there is not type given before the function:

operator= (const Player& other);



this is code inside of the private part of the structure. Why does it not have a type?

What I have tried:

Nothing............................................................

解决方案

This is not a copy constructor.
This is an assignment operator.

Copy constructor signature would be something like LinkedList(const LinkedList& rhs);
Remember constructors don't return anything.

Quote:

is working with references to the variables, why does it need to return anything and where are the return values going?


So that you could do something like list1 = list2 = list3 = list4 = ...

Quote:

he never increments p_itr


You probably need to look inside the insert method to figure out if anything happens there.

Quote:

operator= (const Player& other);


Not correct. In fact this is the signature for a copy constructor like Player(const Player& other);


In a copy contructor you must create a FULL COPY for the new object. So you must copy all member element in a unique matter for the constructed object.

When copying a linked list, you must create a copy each element and put it a new list.

you need:

Player& operator= (const Player& other);



See this operator tutorial.


这篇关于此C ++赋值运算符代码是否不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆