T* get() const noexcept;
|
(C++17 前) | |
element_type* get() const noexcept;
|
(C++17 起) | |
返回存储的指针。
目录 |
(无)
存储的指针。
shared_ptr
可能在存储指向一个对象的指针时共享另一对象的所有权。 get()
返回存储的指针,而非被管理指针。
#include <iostream> #include <memory> #include <string> typedef std::shared_ptr<int> IntPtr; void output(const std::string& msg, int* pInt) { std::cout << msg << *pInt << "\n"; } int main() { int* pInt = new int(42); IntPtr pShared(new int(42)); output("Naked pointer ", pInt); // output("Shared pointer ", pShared); // 编译错误 output("Shared pointer with get() ", pShared.get()); delete pInt; }
输出:
Naked pointer 42 Shared pointer with get() 42
解引用存储的指针 (公开成员函数) |