using swap to make assignment operator exception safe
- From: George George
- Date: Tue, 08 Jan 2008 04:17:01 -0800
Hello everyone,
The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).
It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.
My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?
[Code]
class A;
A& A::operator= (const A& a)
{
A temp;
temp = a; // exception may be thrown
swap (*this, temp);
return *this;
}
[/Code]
thanks in advance,
George
EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
.
- Prev by Date: Re: How to troubleshoot bugchecks on my own?
- Next by Date: unexpected exception handler
- Previous by thread: How to troubleshoot bugchecks on my own?
- Next by thread: unexpected exception handler
- Index(es):
Relevant Pages
|