【发布时间】:2012-11-28 06:02:12
【问题描述】:
我正在阅读an article 关于有效使用auto_ptr 的信息。在那里,建议使用以下代码作为正确的代码:
// Example 10(c): Correct (finally!)
//
auto_ptr<String> f()
{
auto_ptr<String> result = new String;
*result = "some value";
cout << "some output";
return result; // rely on transfer of ownership;
// this can't throw
}
但据我所知,auto_ptr 的赋值运算符只接受另一个auto_ptr 作为rhs——以避免意外误用。那么,下面这行是文章中的错字,还是真的应该起作用?
auto_ptr<String> result = new String;
【问题讨论】:
标签: c++ smart-pointers auto-ptr