C++ unique_ptr::get 得到内部裸指针,仍由智能指针负责释放。只在对接要求 T* 的旧 API 时使用。
原型
pointer get() const noexcept;参数
- 无参数。
返回值
所管对象的指针;空指针表示当前未管对象。
示例
#include <iostream>
#include <memory>
int main() {
auto p = std::make_unique<int>(7);
int* raw = p.get();
std::cout << *raw << "\n";
return 0;
}输出:
7
注意事项
禁止 delete raw。要交出所有权用 release(),之后必须由调用方负责释放。
下一页介绍make_shared。