Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

2992 questions
112
votes
2 answers

Does C++11 unique_ptr and shared_ptr able to convert to each other's type?

Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr, or vice versa? Is this safe operation?
Hind Forsum
  • 8,077
  • 8
  • 40
  • 88
108
votes
4 answers

Where is shared_ptr?

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 is not helping at…
Jake
  • 10,633
  • 20
  • 79
  • 134
107
votes
2 answers

Why is shared_ptr legal, while unique_ptr is ill-formed?

The question really fits in the title: I am curious to know what is the technical reason for this difference, but also the rationale ? std::shared_ptr sharedToVoid; // legal; std::unique_ptr uniqueToVoid; // ill-formed;
Ad N
  • 6,830
  • 4
  • 28
  • 65
99
votes
4 answers

Passing shared_ptr as shared_ptr

What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptrs by reference to avoid a needless copy: int foo(const shared_ptr& ptr); but this…
Matt Kline
  • 9,013
  • 3
  • 44
  • 76
90
votes
5 answers

Passing shared pointers as arguments

If I declare an object wrapped in a shared pointer: std::shared_ptr myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //the called method void…
Steve H
  • 5,219
  • 3
  • 17
  • 26
82
votes
7 answers

Example to use shared_ptr?

Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate…
Ahmed
  • 3,258
  • 11
  • 40
  • 61
82
votes
2 answers

What is the difference between an empty and a null std::shared_ptr in C++?

The cplusplus.com shared_ptr page calls out a distinction between an empty std::shared_ptr and a null shared_ptr. The cppreference.com page doesn't explicitly call out the distinction, but uses both "empty" and comparison to nullptr in its…
R.M.
  • 2,938
  • 1
  • 17
  • 33
76
votes
5 answers

shared_ptr and weak_ptr differences

I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting.…
venkysmarty
  • 10,013
  • 19
  • 87
  • 165
75
votes
3 answers

How does one downcast a std::shared_ptr?

Consider: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true;…
Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
74
votes
4 answers

C++11 When clearing shared_ptr, should I use reset or set to nullptr?

I have a question about C++11 best practices. When clearing a shared_ptr, should I use the reset() function with no parameter, or should I set the shared_ptr to nullptr? For example: std::shared_ptr foo(new…
user1930581
  • 1,235
  • 1
  • 11
  • 22
73
votes
4 answers

static_cast with boost::shared_ptr?

What is the equivalent of a static_cast with boost::shared_ptr? In other words, how do I have to rewrite the following Base* b = new Derived(); Derived* d = static_cast(b); when using shared_ptr? boost::shared_ptr b(new…
Frank
  • 58,417
  • 87
  • 227
  • 317
69
votes
8 answers

Should I switch from using boost::shared_ptr to std::shared_ptr?

I would like to enable support for C++0x in GCC with -std=c++0x. I don't absolutely necessarily need any of the currently supported C++11 features in GCC 4.5 (and soon 4.6), but I would like to start getting used to them. For example, in a few…
Alan Turing
  • 11,403
  • 14
  • 66
  • 114
69
votes
2 answers

Why does unique_ptr take two template parameters when shared_ptr only takes one?

Both unique_ptr and shared_ptr accept a custom destructor to call on the object they own. But in the case of unique_ptr, the destructor is passed as a template parameter of the class, whereas the type of shared_ptr's custom destructor is to be…
qdii
  • 11,387
  • 7
  • 54
  • 107
66
votes
1 answer

Why isn't there a std::shared_ptr specialisation?

The standard provides a template specialization of std::unique_ptr which correctly calls the delete[] from its destructor: void func() { std::unique_ptr< int[] > arr(new int[10]); ....... } With std::shared_ptr this specialisation is not…
mark
  • 6,939
  • 5
  • 34
  • 54
65
votes
7 answers

Why can't a weak_ptr be constructed from a unique_ptr?

If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see…
notadam
  • 2,494
  • 2
  • 16
  • 30