自赋值? [英] Self-assignment?

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

问题描述

如果我尝试为自己分配类类型的变量,我可以期望

(默认)赋值运算符什么都不做?我不是问结果

是否对象不变。我在问它是否会通过

的动作。也就是说,它会触及所有成员字段

并将当前数据分配给自己吗?

-

如果我们的话假设是关于任何东西而不是关于某一个或多个特定的东西,那么我们的推论构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell

解决方案

Steven T. Hatton发布:

如果我尝试将类类型的变量赋给自己,我可以期望
(默认)赋值运算符什么都不做?我不是在询问
结果是否会保持对象不变。我在问它是否会完成动作的任务。也就是说,它会触及所有
成员字段并将当前数据分配给自身吗?




取决于赋值运算符的定义。例如:


AnyClass& AnyClass :: operator =(AnyClass const& other)

{

if(this ==& other)return * this;


//现在做任务

}

或:


AnyClass& AnyClass :: operator =(AnyClass const& other)

{

//现在做任务

}

如果你没有定义operator =,那就好像你定义了后者。

-JKop


Steven T. Hatton写道:

如果我尝试为自己分配类类型的变量,我可以期待
(默认)赋值运算符什么都不做?我不是在问结果
是否会保持对象不变。我在问它是否会完成转让动作。


是的,它会。

也就是说,它会触及所有成员字段并将当前数据分配给自己吗? / blockquote>


如果所有非staic成员变量都是内置类型或者没有定义

他们自己的operator =,编译器可能会优化整件事,

因为没有可观察到的效果。


2004年10月9日星期六18: 16:06 +0200,Rolf Magnus写道:

Steven T. Hatton写道:

如果我尝试分配类类型的变量对于它自己,我可以期待
(默认)赋值运算符什么都不做?我不是在问结果
是否会保持对象不变。我在问它是否会通过
动作。



是的,它会。



这是肯定的它会_always_,或者是允许编译器优化

远离某人正在做什么

Foo f;

f = f;



If I try to assign a variable of class type to itself, can I expect the
(default) assignment operator to do nothing? I''m not asking if the result
will be that the object is unchanged. I''m asking if it will ''go through
the motions'' of assignment. That is, will it touch all the member fields
with an assignment of the current data to itself?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

解决方案

Steven T. Hatton posted:

If I try to assign a variable of class type to itself, can I expect the
(default) assignment operator to do nothing? I''m not asking if the
result will be that the object is unchanged. I''m asking if it will ''go
through the motions'' of assignment. That is, will it touch all the
member fields with an assignment of the current data to itself?



Depends on the definition of the assignment operator. For example:

AnyClass& AnyClass::operator=(AnyClass const &other)
{
if ( this == &other ) return *this;

//Now do assignment
}
or:

AnyClass& AnyClass::operator=(AnyClass const &other)
{
//Now do assignment
}
If you don''t define an "operator=", then it''s as if you defined the latter.
-JKop


Steven T. Hatton wrote:

If I try to assign a variable of class type to itself, can I expect the
(default) assignment operator to do nothing? I''m not asking if the result
will be that the object is unchanged. I''m asking if it will ''go through
the motions'' of assignment.
Yes, it will.
That is, will it touch all the member fields with an assignment of the
current data to itself?



If all the non-staic member variables are of built-in types or don''t define
their own operator=, the compiler might optimize the whole thing away,
since there will be no observable effect.


On Sat, 09 Oct 2004 18:16:06 +0200, Rolf Magnus wrote:

Steven T. Hatton wrote:

If I try to assign a variable of class type to itself, can I expect the
(default) assignment operator to do nothing? I''m not asking if the result
will be that the object is unchanged. I''m asking if it will ''go through
the motions'' of assignment.



Yes, it will.


Is that an yes it will _always_ , or is a compiler allowed to optimize
away someone doing
Foo f;
f = f;
?


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

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