我想知道 ~auto_ptr()
和 release()
函数之间的区别。
是同一种方法,还是销毁和释放内存有区别?
最佳答案
auto_ptr::release()
将 auto_ptr 内部指针设置为空指针(表示它不指向任何对象)而不破坏 auto_ptr 当前指向的对象。它返回内部指针被设置为空之前的值。
在调用 release()
之后,auto_ptr
仍然可以指向另一个对象。
auto_ptr::~auto_ptr()
使用 delete 释放对象“指向”的内存块并销毁对象。\在调用 ~auto_ptr()
之后,auto_ptr
被销毁,不应被销毁进一步使用。
关于c++ - 智能指针(~auto_ptr() 和 release() 不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6009294/