public member function
<memory>

std::shared_ptr::get

element_type* get() const noexcept;
Get pointer
Returns the stored pointer.

The stored pointer points to the object the shared_ptr object dereferences to, which is generally the same as its owned pointer.

The stored pointer (i.e., the pointer returned by this function) may not be the owned pointer (i.e., the pointer deleted on object destruction) if the shared_ptr object is an alias (i.e., alias-constructed objects and their copies).

Parameters

none

Return value

The stored pointer.
element_type is a member type, alias of shared_ptr's template parameter (T).

Example

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;
}

Output:
a and p point to the same location
10
10
10


See also