如何在operator =函数中重用复制构造函数 [英] how to reuse a copy constructor in an operator = function

查看:60
本文介绍了如何在operator =函数中重用复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的同事为一个类编写了一个复制构造函数。现在我需要在类中添加

一个operator =。有没有办法做到这一点而不用改变她的代码

(复制构造函数)呢?非常感谢您的帮助。


JD

Hi,

My associate has written a copy constructor for a class. Now I need to add
an operator = to the class. Is there a way to do it without change her code
(copy constructor) at all? Your help is much appreciated.

JD

推荐答案

" JD" < jd ******* @ yahoo.comwrote:
"JD" <jd*******@yahoo.comwrote:

我的同事为一个类编写了一个拷贝构造函数。现在我需要在类中添加

一个operator =。有没有办法做到这一点而不用改变她的代码

(复制构造函数)呢?非常感谢您的帮助。
My associate has written a copy constructor for a class. Now I need to add
an operator = to the class. Is there a way to do it without change her code
(copy constructor) at all? Your help is much appreciated.



是的。尝试一下,我相信我们可以为你批评它。

Yes. Make the attempt and I''m sure we can critique it for you.


JD写道:
JD wrote:

我的同事为一个类编写了一个复制构造函数。现在我需要

在类中添加一个operator =。有没有办法不做更改

她的代码(复制构造函数)?非常感谢您的帮助。
My associate has written a copy constructor for a class. Now I need to
add an operator = to the class. Is there a way to do it without change
her code (copy constructor) at all? Your help is much appreciated.



首先,为什么添加赋值运算符会影响副本

构造函数?实际上,你很可能用复制构造函数来实现赋值。有两种方法:


(a)复制交换:


T& operator =(T const& t){

T(t).swap(* this);

return(* this);

}


你需要为这个实现一个swap()方法。

(b)Destruct-resurrect:


T& operator =(T const& t){

if(& t!= this){

this-> T :: ~T();

new(this)T(t);

}

return(* this);

}


这种方法有问题,并且不是例外,因此并非所有情况下都适用(但是尽管H.Sutter发现了严厉的话语,否则) b $ b关于它,有时候可以说是合理的。)

经验法则:一般(总是,当有疑问时),请跟(a)一起去。

Best


Kai-Uwe Bux

First off, why would adding an assignment operator affect the copy
constructor? In fact, you can very likely implement assignment in terms of
the copy-constructor. There are two methods:

(a) Copy-swap:

T& operator= ( T const & t ) {
T(t).swap( *this );
return ( *this );
}

You need to implement a swap() method for this one.
(b) Destruct-resurrect:

T& operator= ( T const & t ) {
if ( &t != this ) {
this->T::~T();
new (this) T (t);
}
return ( *this );
}

This method has gotchas and is not exception safe and is therefore not
applicable in all cases (but despite the harsh words that H. Sutter finds
about it, it can be justified sometimes).
Rule of thumb: generically (and always, when in doubt), go with (a).
Best

Kai-Uwe Bux


11月2日晚上10点10分,Kai-Uwe Bux< jkherci ...... @ gmx.netwrote:
On Nov 2, 10:10 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

JD写道:
JD wrote:

我的同事为类写了一个复制构造函数。

现在我需要在类中添加一个operator =。是否有一个

的方法来完成它而不需要更改她的代码(复制构造函数)

all?非常感谢您的帮助。
My associate has written a copy constructor for a class.
Now I need to add an operator = to the class. Is there a
way to do it without change her code (copy constructor) at
all? Your help is much appreciated.


首先,为什么添加赋值运算符会影响

复制构造函数?实际上,您很可能在复制构造函数方面实现

赋值。有两种方法:
First off, why would adding an assignment operator affect the
copy constructor? In fact, you can very likely implement
assignment in terms of the copy-constructor. There are two
methods:


(a)复制交换:
(a) Copy-swap:


T& operator =(T const& t){

T(t).swap(* this);

return(* this);

}
T& operator= ( T const & t ) {
T(t).swap( *this );
return ( *this );
}


您需要为此实现swap()方法。
You need to implement a swap() method for this one.


(b)Destruct-resurrect:
(b) Destruct-resurrect:


T& operator =(T const& t){

if(& t!= this){

this-> T :: ~T();

new(this)T(t);

}

返回(* this);

}
T& operator= ( T const & t ) {
if ( &t != this ) {
this->T::~T();
new (this) T (t);
}
return ( *this );
}


这种方法有问题并且不是例外安全因此

因此并不适用于所有情况(但尽管严酷
H. Sutter发现的词,它可以被证明是有道理的

有时候)。
This method has gotchas and is not exception safe and is
therefore not applicable in all cases (but despite the harsh
words that H. Sutter finds about it, it can be justified
sometimes).



如果有人从

类继承,它也会导致无问题。

It also causes no end of problems if someone inherits from the
class.


经验法则:一般(总是,当有疑问时),用(a)去


Rule of thumb: generically (and always, when in doubt), go
with (a).



只有你可以实现一个无抛出的swap版本。这通常不是
,如果交换抛出

中间的某个地方,你可能最终会出现一个不连贯的状态。 (另外,我好b / b更喜欢更明确的一点:


T&

T :: operator =(T const& other )

{

T tmp(其他);

swap(tmp);

返回* this; < br $>
}


..有点清楚,恕我直言。)


一般来说,当然,实现的规则强大的

异常安全保证(事务完整性)是指在修改任何

状态之前可能引发异常的所有内容。但是你并不需要经常提供强有力的保证,而且遇到它可能会很昂贵。


-

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

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9个地方Sémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34

Only if you can implement a no-throw version of swap. This is
not generally the case, and if swap throws somewhere in the
middle, you may end up with an incoherent state. (Also, I
prefer the somewhat more explicit:

T&
T::operator=( T const& other )
{
T tmp( other ) ;
swap( tmp ) ;
return *this ;
}

.. Somewhat clearer, IMHO.)

In general, of course, the rule for achieving the strong
exception safety guarantee (transactional integrity) is to do
everything that might raise an exception before modifying any
state. But you don''t really need the strong guarantee that
often, and meeting it can be expensive.

--
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


这篇关于如何在operator =函数中重用复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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