std::shared_ptr::get (3) Linux Manual Page
std::shared_ptr<T>::get – std::shared_ptr<T>::get
Synopsis
T* get() const noexcept; (until C++17)
element_type* get() const noexcept; (since C++17)
Returns the stored pointer.
Parameters
(none)
Return value
The stored pointer.
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.
Example
// Run this code
#include <iostream>
#include <memory>
#include <string_view>
void output(std::string_view msg, int const *pInt)
{
std::cout << msg << *pInt << "\n";
}
int main()
{
int *pInt = new int(42);
std::shared_ptr<int> pShared = std::make_shared<int>(42);
output("Naked pointer ", pInt);
// output("Shared pointer ", pShared); // compiler error
output("Shared pointer with get() ", pShared.get());
delete pInt;
}
Output:
See also
operator* (public member function)
operator->
