本文介绍了为什么引用类型成员导致隐式声明的副本分配运算符被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自

已删除的隐式声明的副本分配运算符
为T定义的隐式声明的或默认的副本赋值运算符定义为在以下任何情况下均已删除:

Deleted implicitly-declared copy assignment operatorThe implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:

T has a non-static data member that is const
T has a non-static data member of a reference type.
T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator)
T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator)
T has a user-declared move constructor
T has a user-declared move assignment operator 

那可以告诉我是什么原因导致删除,而不是为什么?谁能解释一下:

So that tells me what causes the deletion but not the why? Can anyone explain for:

T has a non-static data member of a reference type.

以及在我的课程中是否足以处理已删除的运算符:

and whether this will suffice in my class to deal with the deleted operator:

T& T:operator=(T& t){};

如果我有一个引用类型的基类成员。

if I have a member of a base class which is a reference type.

我是否需要在 operator = 中做任何事情,例如明确声明return *这个还是编译器(g ++)为我处理这个问题?我需要对参考会员做些特别的事情吗?抱歉,没有菜鸟问题,但是我对C ++还是陌生的,因为C ++始于托管语言(C#和Java)。

Do I need to do anything in my operator= such as explicitly declare return *this or will the compiler (g++) handle this for me? Do I have to do anything special with the reference member? Sorry for noob question but I am new to C++ having started off with managed languages (C# and Java).

推荐答案

引用已绑定当它们被初始化后再绑定到一个对象,之后再也不能更改,因此您对它们执行的所有其他操作都会影响它们所绑定的对象,而不是引用本身。

References are bound to an object when they are initialized and can never be altered after that, everything else you do to them affects the object they are bound to, not the reference itself.

因此参考构件在构造过程中已设置,并且从未更改。由于赋值运算符的目的是在构造后更改成员,因此在永远无法更改成员之一时,生成隐式赋值运算符是没有意义的。编译器拒绝尝试猜测您要执行的操作,并强迫您向自己的赋值运算符提供所需的语义。

So a reference member is set during construction, and never altered. Since the purpose of an assignment operator is to alter members after construction, it doesn't make sense to generate an implicit assignment operator when one of the member can never be altered. The compiler refuses to try and guess what you want it to do and forces you to provide your own assignment operator with the semantics you want.

您绝对肯定100%需要返回* this;

You absolutely definitely 100% need to return *this;

唯一一次您不需要C ++中的显式返回是如果函数返回 void main()(其中存在隐式的返回0; (如果到达函数末尾)或在异常情况下,例如永不返回的函数(永远循环或抛出异常)。

The only time you don't need an explicit return in C++ is if your function returns void or in main() (where there is an implicit return 0; if you reach the end of the function) or in unusual cases such as functions that never return (either looping forever or throwing an exception).

如果您不希望这样做,则取决于您希望对类型进行赋值的语义。 o更改引用所绑定的对象,很好,不要执行任何操作。

If you don't want it to change the object the reference is bound to, fine, do nothing with it.

如果您想分配以更改引用所绑定的对象,则需要执行

If you want assignment to alter the object the reference is bound to, you need to do that.

如果您希望将引用重新绑定到另一个对象,那么您就不走运了,C ++不允许这样做。

If you want the reference to be re-bound to a different object, you're out of luck, C++ doesn't allow that.

这篇关于为什么引用类型成员导致隐式声明的副本分配运算符被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:16