且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用引用更新智能指针

更新时间:2023-11-08 23:19:22

分配对象到 std :: shared_ptr toy 将在其范围结束时被销毁, std :: shared_ptr 将尝试删除不是的东西。 std :: shared_ptr 必须是动态分配的对象的地址(虽然可以提供自定义删除器,但不是

Don't pass the address of a stack allocated object to a std::shared_ptr. toy will be destructed at the end of its scope and the std::shared_ptr will attempt to delete something that was not newd. The address held by a std::shared_ptr must be that of a dynamically allocated object (although a custom deleter can be provided, but that is not the case here).

要更改 std :: shared_ptr 正在管理的对象,请使用 std :: shared_ptr :: reset()

To change the object that a std::shared_ptr is managing use std::shared_ptr::reset().