std::shared_ptr::get

T* get() const;
(since C++11)

Returns a pointer to the managed object or a null pointer if no object is being managed.

Parameters

(none).

Return value

A pointer to the managed object or null.

Exceptions

noexcept specification:
noexcept

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

See also

operator*operator->
dereferences pointer to the managed object
(public member function)
doc_CPP
2016-10-11 10:06:38
Comments
Leave a Comment

Please login to continue.