I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr
(and working). Simply stating std
, tr1
and <memory>
is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by telling exactly where to find it?
Thanks for letting me vent my frustrations!
EDIT: I see my title has been changed. Sorry about that. So... it was also because it was not clear to me that shared_ptr is "C++ version dependant" --> that's why I did not state my environment --> therefore probably why it was so difficult for me to find it.
I am working on MSVS2008.
EDIT 2: I don't know why, but I was including [memory] and [boost/tr1/memory.hpp] and [boost/tr1/tr1/memory] while looking everywhere for the shared_ptr.. of course, i couldn't.
Thanks for all the responses.
The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.
shared_ptr objects can only share ownership by copying their value: If two shared_ptr are constructed (or made) from the same (non-shared_ptr) pointer, they will both be owning the pointer without sharing it, causing potential access problems when one of them releases it (deleting its managed object) and leaving the ...
Use shared_ptr if you want to share ownership of a resource. Many shared_ptr can point to a single resource. shared_ptr maintains reference count for this propose. when all shared_ptr's pointing to resource goes out of scope the resource is destroyed.
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.
There are at least three places where you may find shared_ptr
:
If your C++ implementation supports C++11 (or at least the C++11 shared_ptr
), then std::shared_ptr
will be defined in <memory>
.
If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr
will likely be in <memory>
(Microsoft Visual C++) or <tr1/memory>
(g++'s libstdc++). Boost also provides a TR1 implementation that you can use.
Otherwise, you can obtain the Boost libraries and use boost::shared_ptr
, which can be found in <boost/shared_ptr.hpp>
.
Boost Getting Started
If you want to use it from Boost TR1 instead
shared_ptr Example
for VS2008 with feature pack update, shared_ptr can be found under namespace std::tr1.
std::tr1::shared_ptr<int> MyIntSmartPtr = new int;
of
if you had boost installation path (for example @ C:\Program Files\Boost\boost_1_40_0
) added to your IDE settings:
#include <boost/shared_ptr.hpp>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With