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
1614
votes
17 answers

When to use virtual destructors?

I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object in the chain. When are you meant to make them…
Lodle
  • 28,027
  • 19
  • 59
  • 89
306
votes
10 answers

Should we pass a shared_ptr by reference or by value?

When a function takes a shared_ptr (from boost or C++11 STL), are you passing it: by const reference: void foo(const shared_ptr& p) or by value: void foo(shared_ptr p) ? I would prefer the first method because I suspect it would be faster.…
Danvil
  • 20,344
  • 18
  • 61
  • 85
304
votes
8 answers

Difference in make_shared and normal shared_ptr in C++

std::shared_ptr p1 = std::make_shared("foo"); std::shared_ptr p2(new Object("foo")); Many google and stackoverflow posts are there on this, but I am not able to understand why make_shared is more efficient than directly…
Anup Buchke
  • 4,300
  • 5
  • 19
  • 34
292
votes
14 answers

When is std::weak_ptr useful?

I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr. Can someone tell me when std::weak_ptr is useful/necessary?
user1434698
269
votes
4 answers

Differences between unique_ptr and shared_ptr

Possible Duplicates: pimpl: shared_ptr or unique_ptr smart pointers (boost) explained Could someone explain differences between shared_ptr and unique_ptr?
smallB
  • 14,808
  • 29
  • 97
  • 144
205
votes
15 answers

How do I call ::std::make_shared on a class with only protected or private constructors?

I have this code that doesn't work, but I think the intent is clear: testmakeshared.cpp #include class A { public: static ::std::shared_ptr create() { return ::std::make_shared(); } protected: A() {} A(const A &)…
Omnifarious
  • 50,447
  • 15
  • 117
  • 181
184
votes
2 answers

shared_ptr to an array : should it be used?

Just a small query regarding shared_ptr. Is it a good practice to use shared_ptr pointing to an array? For example, shared_ptr sp(new int[10]); If not, then why not? One reason I am already aware of is one can not increment/decrement the…
tshah06
  • 2,225
  • 3
  • 16
  • 21
169
votes
2 answers

Using smart pointers for class members

I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr/weak_ptr work in general. What I don't understand is the real…
michaelk
  • 1,947
  • 3
  • 14
  • 18
166
votes
6 answers

Why would I std::move an std::shared_ptr?

I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr?…
sdgfsdh
  • 24,047
  • 15
  • 89
  • 182
132
votes
6 answers

Why do std::shared_ptr work

I found some code using std::shared_ptr to perform arbitrary cleanup at shutdown. At first I thought this code could not possibly work, but then I tried the following: #include #include #include class test { public: …
LiKao
  • 10,010
  • 5
  • 47
  • 85
126
votes
4 answers

Difference between `const shared_ptr` and `shared_ptr`?

I'm writing an accessor method for a shared pointer in C++ that goes something like this: class Foo { public: return_type getBar() const { return m_bar; } private: boost::shared_ptr m_bar; } So to support the const-ness of…
Dave Lillethun
  • 2,598
  • 2
  • 17
  • 24
119
votes
3 answers

Should I pass a shared_ptr by reference?

What are the best practices for passing a shared_ptr? Currently I pass shared_ptr function arguments like so: void function1( shared_ptr& value );
Ben Crowhurst
  • 7,042
  • 5
  • 40
  • 72
117
votes
3 answers

std::shared_ptr thread safety explained

I'm reading http://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html and some thread safety issues are still not clear for me: Standard guarantees that reference counting is handled thread safe and it's platform independent, right? Similar…
Goofy
  • 4,637
  • 4
  • 34
  • 55
115
votes
17 answers

C++ - passing references to std::shared_ptr or boost::shared_ptr

If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the…
abigagli
  • 2,740
  • 3
  • 28
  • 32
114
votes
2 answers

std::shared_ptr of this

I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution: Imagine you have an object of class A being parent of an object of…
Icarus
  • 1,455
  • 2
  • 11
  • 12
1
2 3
99 100