Namespaces
Variants
Views
Actions

std::shared_ptr::get

From cppreference.com
< cpp‎ | memory‎ | shared ptr
Revision as of 14:58, 3 April 2013 by Jbenshetler (Talk | contribs)

 
 
 
 
 
T* get() const;

Returns a pointer to the managed object.

Contents

Parameters

(none)

Return value

a pointer to the managed object.

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 argc,char* argv[])
{
    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;
    return 0;
}

Output:

Naked pointer 42
Shared pointer with get() 42

See also

Template:cpp/memory/shared ptr/dcl list operator*