std::shared_ptr::get

From cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
 
 
T* get() const;
(until C++17)
element_type* get() const;
(since C++17)

Returns the stored pointer.

Contents

[edit] Parameters

(none)

[edit] Return value

The stored pointer.

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] Notes

A shared_ptr may share ownership of an object while storing a pointer to another object. get() returns the stored pointer, not the managed pointer.

[edit] Example

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

Output:

Naked pointer 42
Shared pointer with get() 42

[edit] See also

dereferences the stored pointer
(public member function)