public member function
<memory>

std::shared_ptr::get

element_type* get() const noexcept;
獲取指標
返回儲存的指標

儲存的指標指向 shared_ptr 物件 解引用的目標物件,這通常與其擁有的指標相同。

shared_ptr 物件為別名(即透過別名構造的物件及其副本)時,儲存的指標(即此函式返回的指標)可能與其擁有的指標(即物件銷燬時刪除的指標)不同。

引數



返回值

儲存的指標
element_type是成員型別,是 shared_ptr 的模板引數的別名(T).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// shared_ptr::get example
#include <iostream>
#include <memory>

int main () {
  int* p = new int (10);
  std::shared_ptr<int> a (p);

  if (a.get()==p)
    std::cout << "a and p point to the same location\n";

  // three ways of accessing the same address:
  std::cout << *a.get() << "\n";
  std::cout << *a << "\n";
  std::cout << *p << "\n";

  return 0;
}

輸出
a and p point to the same location
10
10
10


另見