S
stf
http://www.parashift.com/c++-faq-lite/assignment-operators.html#faq-12.3
Fred& Fred:perator= (const Fred& f)
{
// This code gracefully (albeit implicitly) handles self assignment
Wilma* tmp = new Wilma(*f.p_); // It would be OK if an exception
got thrown here
delete p_;
p_ = tmp;
return *this;
}
What if an exception was thrown in
delete p_;
? (It could, because this statement involves calling the destructor of
Wilma, right?) Would the memory region pointed to by p_ remain
allocated? (Here I assume Wilma could be more complicated than in the
example.)
Probably a better solution would be:
Wilma* tmp = p_;
p_ = NULL;
delete tmp;
tmp = new Wilma(*f.p_);
p_ = tmp;
return *this;
?
Thanks,
STF
Fred& Fred:perator= (const Fred& f)
{
// This code gracefully (albeit implicitly) handles self assignment
Wilma* tmp = new Wilma(*f.p_); // It would be OK if an exception
got thrown here
delete p_;
p_ = tmp;
return *this;
}
What if an exception was thrown in
delete p_;
? (It could, because this statement involves calling the destructor of
Wilma, right?) Would the memory region pointed to by p_ remain
allocated? (Here I assume Wilma could be more complicated than in the
example.)
Probably a better solution would be:
Wilma* tmp = p_;
p_ = NULL;
delete tmp;
tmp = new Wilma(*f.p_);
p_ = tmp;
return *this;
?
Thanks,
STF