Richard Gillam 在他的“赋值运算符的剖析”中,当他在论文的开头说了以下内容时,他可能做出了错误的陈述:
“这个问题的一个正确答案应该是这样的:”
TFoo&TFoo::operator=(const TFoo& that)
{
if (this != &that)
{
TBar* bar1 = 0;
TBar* bar2 = 0;
try
{
bar1 = new TBar(*that.fBar1);
bar2 = new TBar(*that.fBar2);
}
catch (...)
{
delete bar1;
delete bar2;
throw;
}
TSuperFoo::operator=(that);
delete fBar1;
fBar1 = bar1;
delete fBar2;
fBar2 = bar2;
}
return *this;
}
我认为作者是错误的,因为如果TSuperFoo::operator=()
抛出,bar1
并且bar2
会泄漏。