以下两段代码有何不同?哪种更可取?
operator=
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo = boost::shared_ptr<Blah>(new Blah()); // Involves creation and copy of a shared_ptr?
重置
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo.reset(new Blah()); // foo.ptr should point now to a new Blah object
注意:我需要先定义shared_ptr,然后在另一行中设置它,因为我将在代码片段中使用它,如下所示:
boost::shared_ptr<Blah> foo;
try
{
foo.reset...
}
foo...