std::shared_ptr::get
T* get() const; | | (until C++17) |
---|---|---|
element_type* get() const; | | (since C++17) |
返回存储的指针。
参数
%280%29
返回值
存储的指针。
例外
noexcept
规格:
noexcept
注记
阿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); // compiler error
output("Shared pointer with get() ", pShared.get());
delete pInt;
}
二次
产出:
二次
Naked pointer 42
Shared pointer with get() 42
二次
另见
operator*operator-> | dereferences the stored pointer (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com