\n"; delete p; } }; int main() { std::cout << "创建新的 Foo...\n"; std::unique_ptr<Foo, D> up(new Foo(), D()); // up 占有 Foo 指针(删除器 D) std::cout << "以新的 Foo 替换所拥有的 Foo...\n"; up.reset(new Foo()); // 调用旧者的删除器 std::cout << "...
unique_ptr的产生,就是为了解决,raw pointer 的new和delete配对使用问题。对于raw pointer来说,在new...
new Foo...\n";std::unique_ptr<Foo,D>up(newFoo(),D());// up owns the Foo pointer (deleter D)std::cout<<"Replace owned Foo with a new Foo...\n";up.reset(newFoo());// calls deleter for the old onestd::cout<<"Release and delete the owned Foo...\n";up.reset(nullptr)...
voidreset(pointer ptr=pointer())noexcept; (1)(constexpr since C++23) members of the specialization unique_ptr<T[]> template<classU> voidreset(U ptr)noexcept; (2)(constexpr since C++23) voidreset(std::nullptr_t=nullptr)noexcept;
主模板 unique_ptr<T> 的成员 void reset( pointer ptr = pointer() ) noexcept; (1) 特化unique_ptr<T[]> 的成员 template< class U >void reset( U ) noexcept; (2) void reset( std::nullptr_t p = nullptr ) noexcept; (3) 替换被管理对象。1) 给定指向 *this 所管理对象的指针 cu...