std::shared_ptr::get

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

参阅

解引用存储的指针
(公开成员函数)

版本历史

  • (当前 | 先前 2017年9月3日 (日) 18:48Fruderica讨论 | 贡献. . (1,157字节) (+431). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (726字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:06P12bot讨论 | 贡献 . . (726字节) (-25). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:07P12bot讨论 | 贡献 . . (751字节) (+265). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:40P12讨论 | 贡献 . . (486字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (486字节) (-39). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:55P12讨论 | 贡献 . . (525字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (525字节) (+525). . (Translated from the English version using Google Translate)