首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::shared_ptr::get

T* get() const;

(until C++17)

element_type* get() const;

(since C++17)

返回存储的指针。

参数

%280%29

返回值

存储的指针。

例外

noexcept规格:

noexcept

注记

shared_ptr可以在存储指向另一个对象的指针时共享对象的所有权。get()返回存储的指针,而不是托管指针。

二次

代码语言:javascript
复制
#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;
}

二次

产出:

二次

代码语言:javascript
复制
Naked pointer 42
Shared pointer with get() 42

二次

另见

operator*operator->

dereferences the stored pointer (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券