分配操作员自我分配检查 [英] Assignment operator self-assignment check

查看:56
本文介绍了分配操作员自我分配检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有理由将此声明为


if(* this == rhs)


与我通常的情况相反看看


if(this ==& rhs)





好​​像是以前的版本会更昂贵,而不是简单地比较后者的地址(加上前者需要

类来定义operator ==,不是吗? )。想知道是否添加

的努力是否合理。

解决方案

Chris写道:
< blockquote class =post_quotes>
是否有理由将此声明为


if(* this == rhs)


,而不是我通常看到的


if(this ==& rhs)





自我分配检查检测对象标识,C ++将其指定为

对象的地址。


将值复制到已经具有

该值的对象的退化情况不太重要。让它发生;不要总是浪费CPU

循环检查它。


-

Phlip
http://www.greencheese.us/ZeekLand < - 不是博客!!!


Chris写道:


是否有理由将此声明为

if(* this == rhs)


与我通常看到的相反


if(this ==& ; rhs)





好​​像以前的版本会更贵而不是

比较后者中的地址(加上前者需要

类来定义operator ==,不是吗?)。想知道是否添加了

的努力是否合理。



好​​吧,只要我们处理假设:如果(1)检查

类的等价是否便宜,( 2)经常检查相等性

评估为真,即使它不是同一个对象(在

占用相同地址的意义上),以及(3)使用复制构造函数是非常昂贵的,然后可以想象使用前一版本,因为它将会b / b
避免不必要的复制结构。


后一版本应该是默认版本。


祝你好运,


Tom


Chris发布:


是否有理由将此声明为


if(* this == rhs)



这不是声明 - 请仔细选择你的话。


而不是我通常看到的


if(this ==&a mp; rhs)



后者比较两个对象的ADDRESSES,而前者比较两个对象本身的




好​​像以前的版本会更贵而不是

简单地比较后者的地址(加上前者要求

用于定义operator ==的类,不是吗?)。



是的,前者需要一个可访问的运营商==。


想知道是否额外的努力是有道理的。



取决于多种因素:

(1)课程实施方式

(2复制它是多么昂贵

(3)比较它是否平等是多么昂贵

(4)物体与自身比较的频率


如果班上的一个对象永远不能分配给自己,我会

建议断言:


#include < cassert>


MyClass& MyClass :: operator =(MyClass const& rhs)

{

assert(这个!=& rhs);


/ *现在执行作业* /


返回* this;

}


-


Frederick Gotham


Is there ever a reason to declare this as

if(*this == rhs)

as opposed to what I normally see

if(this == &rhs)

?

Seems like the former version is going to be more expensive rather than
simply comparing addresses as in the latter (plus the former requires
the class to define operator== as well, no?). Wondering if that added
effort is ever justified.

解决方案

Chris wrote:

Is there ever a reason to declare this as

if(*this == rhs)

as opposed to what I normally see

if(this == &rhs)

?

The self-assignment check detects object identity, which C++ specifies as an
object''s addresses.

The degenerate situation of copying a value into an object which already had
that value is less important. Just let it happen; don''t always waste CPU
cycles checking for it.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


Chris wrote:

Is there ever a reason to declare this as

if(*this == rhs)

as opposed to what I normally see

if(this == &rhs)

?

Seems like the former version is going to be more expensive rather than
simply comparing addresses as in the latter (plus the former requires
the class to define operator== as well, no?). Wondering if that added
effort is ever justified.

Well, as long as we''re dealing with hypotheticals: If (1) checking the
class for equality is cheap, (2) checking for equality frequently
evaluates to true even though it''s not the same object (in the sense of
occuping the same address), and (3) using the copy constructor is very
expensive, then one can imagine using the former version, since it will
avoid unnecessary copy constructions.

The latter version ought to be the default, though.

Best regards,

Tom


Chris posted:

Is there ever a reason to declare this as

if(*this == rhs)


That is not a declaration -- choose your words carefully.

as opposed to what I normally see

if(this == &rhs)


The latter compares the ADDRESSES of two objects, while the former compares
the two objects themselves.

Seems like the former version is going to be more expensive rather than
simply comparing addresses as in the latter (plus the former requires
the class to define operator== as well, no?).


Yes, the former requires an accessible operator==.

Wondering if that added effort is ever justified.


Depends on a wonderous amount of factors:
(1) How the class is implemented
(2) How expensive it is to copy it
(3) How expensive it is to compare it for equality
(4) How often an object is compared to itself

If an object of the class should NEVER be assigned to itself, I would
suggest an assert:

#include <cassert>

MyClass &MyClass::operator=(MyClass const &rhs)
{
assert(this != &rhs);

/* Now perform assignment */

return *this;
}

--

Frederick Gotham


这篇关于分配操作员自我分配检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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