重载赋值运算符 [英] Overloaded assignment operator

查看:98
本文介绍了重载赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历Deitel& Deitel的C ++书籍(

第四版第8.8节),其中他们构建了一个Array类,并展示了如何重载运算符
。赋值运算符过载,因为

如下:

const Array& operator =(const Array&);


根据D& D,常规回报旨在避免(a1 = a2)=

a3。我的问题是:


1)为什么这是必要的?毕竟,像(a1 = a2)= a3

这样的作业适用于普通变量。


2)如果你想在这个方法上使用这个方法怎么办?非常量Array对象,

或者你想要它返回一个非常量数组吗?我可以看到它仍然是

有效,但是为什么const声明不会妨碍?


谢谢。

解决方案

Bruno Panetta写道:


我正在经历Deitel& Deitel的C ++书籍(

第四版第8.8节),其中他们构建了一个Array类,并展示了如何重载运算符
。赋值运算符过载,因为

如下:

const Array& operator =(const Array&);


根据D& D,常规回报旨在避免(a1 = a2)=

a3。我的问题是:


1)为什么这是必要的?毕竟,像(a1 = a2)= a3

这样的赋值适用于普通变量。



没有必要。这是一个设计决策,也是一个值得商榷的决定。注意,

标准描述了表64中的可分配要求,如下所示:

表达式t = u必须具有等于u
的后置条件
和返回类型T&。因此,赋值运算符

返回const引用的类不可分配。因此,像


std :: vector<数组>


不需要编译。 (虽然在实践中,只有最热心的

概念检查库才能实现。)

另请注意,标准容器都有一个赋值运算符

返回一个非const引用(实际上,这是一个容器需求)。我没有看到离开这种模式的充分理由。


2)如果你想要什么在非常量Array对象上使用此方法,



没问题。


或者如果你想要的话它返回一个非常数数组?



然后你应该使返回类型为非const。


我可以看到它仍然有效,但是为什么const声明不会以

的方式进入?



嗯?


请提供一段代码来说明您的后顾之忧。

Best


Kai-Uwe Bux


10月18日上午8:44,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:


Bruno Panetta写道:


我正在经历Deitel& Deitel的C ++书籍(

第四版第8.8节),其中他们构建了一个Array类,并展示了如何重载运算符
。赋值运算符过载,因为
如下:


const Array & operator =(const Array&);


根据D& D,const return旨在避免(a1 = a2) =

a3。我的问题是:


1)为什么这有必要?毕竟,像(a1 = a2)= a3

这样的赋值适用于普通变量。



请注意,上面的赋值*不适用于基本类型或

指针,而是未定义行为(在C ++中 - 在C中,它不应该编译
)。


没有必要。这是一个设计决定,也是一个值得商榷的问题。请注意,该标准描述了表64中的Assignable

要求如下:表达式t = u必须

的后置条件t等于u和

返回类型T&。



Deitel& amp;在STL普遍使用之前,Deitel写了他们的作品

。考虑到STL的限制,今天,我不认为返回类型应该是T&对

这个事实有任何疑问,并且不是T const&。

在STL变得广泛传播之前,讨论中有相当大的空间

:const引用的返回值接近

你可以来模拟内置类型的C行为。

许多人不同意C ++放松规则

这里。这里使用const引用是一个相当宽的

传播约定。


它遵循赋值运算符返回的类

const引用不可分配。因此,

就像


std :: vector<数组>


不需要编译。 (虽然在实践中,只有

最热心的概念检查库才能解决这个问题。)



有人可能会认为它是过度指定的。恕我直言,可分配

应该只要求分配按预期工作,没有

对返回值类型的实际约束(除了

可以安全地忽略它。


另请注意,标准容器都有一个赋值

运算符,它返回一个非const引用(事实上​​,这是一个

的集装箱要求)。我没有看到从这种模式中离开

的充分理由。



不脱离一般模式是一个不同的论点。

我自己提前删除了const,只是为了与

内置类型,即使它只影响编程

样式,我不想开始。


- -

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


I am going through Deitel & Deitel''s C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:

const Array &operator=(const Array &);

According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:

1) Why is this necessary? After all, an assignment like (a1 = a2) = a3
works for ordinary variables.

2) What if you want to use this method on a non-constant Array object,
or if you want it to return a non-constant Array? I can see it still
works, but why don''t the const declarations get in the way?

Thanks.

解决方案

Bruno Panetta wrote:

I am going through Deitel & Deitel''s C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:

const Array &operator=(const Array &);

According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:

1) Why is this necessary? After all, an assignment like (a1 = a2) = a3
works for ordinary variables.

It is not necessary. It''s a design decision, and a debatable one. Note that
the standard describes the Assignable requirement in Table 64 as follows:
the expression t=u has to have the postcondition that t be equivalent to u
and the return type T&. It follows that classes whose assignment operator
returns a const reference are not assignable. Consequently, something like

std::vector< Array >

is not required to compile. (Although in practice, only the most zealous
concept checking libraries will snap at that.)
Also note that the standard containers all have an assignment operator that
return a non-const reference (in fact, this is a container requirement). I
don''t see a good reason to depart from this pattern.

2) What if you want to use this method on a non-constant Array object,

No problem.

or if you want it to return a non-constant Array?

Then you should make the return type non-const.

I can see it still works, but why don''t the const declarations get in the
way?

Huh?

Please provide a piece of code that illustrates your worries.
Best

Kai-Uwe Bux


On Oct 18, 8:44 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Bruno Panetta wrote:

I am going through Deitel & Deitel''s C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:

const Array &operator=(const Array &);

According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:

1) Why is this necessary? After all, an assignment like (a1 = a2) =a3
works for ordinary variables.

Note that the above assignment *doesn''t* work for basic types or
pointers, but rather has undefined behavior (in C++ -- in C, it
shouldn''t compile).

It is not necessary. It''s a design decision, and a debatable
one. Note that the standard describes the Assignable
requirement in Table 64 as follows: the expression t=u has to
have the postcondition that t be equivalent to u and the
return type T&.

It''s entirely possible that Deitel & Deitel wrote their work
before the STL became generally used. Given the constraints of
the STL, today, I don''t think that there''s any question to the
fact that the return type should be T&, and not T const&.
Before the STL became wide spread, there was considerable room
for discussion: the return of a const reference is as close as
you can come to similating the C behavior of the built-in types.
And many people don''t agree with C++''s loosening of the rules
here. The use of a const reference here was a fairly wide
spread convention in earlier days.

It follows that classes whose assignment operator returns a
const reference are not assignable. Consequently, something
like

std::vector< Array >

is not required to compile. (Although in practice, only the
most zealous concept checking libraries will snap at that.)

One could argue that it is overspecified. IMHO, Assignable
should only require that assignment works as expected, with no
real constraints on the type of the return value (other than
that it can safely be ignored).

Also note that the standard containers all have an assignment
operator that return a non-const reference (in fact, this is a
container requirement). I don''t see a good reason to depart
from this pattern.

Not departing from the general pattern is a different argument.
I dropped the const early myself, simply to be compatible with
the built-in types, even though it only affects programming
styles that I don''t like to begin with.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于重载赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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