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.

3323 questions
1863
votes
20 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
  • 31,277
  • 19
  • 64
  • 91
385
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
  • 5,210
  • 5
  • 22
  • 38
366
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
  • 22,240
  • 19
  • 65
  • 88
358
votes
15 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
342
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
  • 16,662
  • 33
  • 107
  • 151
244
votes
19 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
  • 54,333
  • 19
  • 131
  • 194
209
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,625
  • 3
  • 19
  • 23
208
votes
8 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
  • 33,689
  • 26
  • 132
  • 245
185
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
  • 2,107
  • 3
  • 15
  • 18
156
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,978
  • 3
  • 20
  • 24
149
votes
3 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
  • 9,717
  • 13
  • 63
  • 119
145
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
  • 5,187
  • 5
  • 40
  • 56
142
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,746
  • 2
  • 13
  • 12
141
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
  • 8,204
  • 6
  • 48
  • 78
141
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
  • 10,149
  • 7
  • 50
  • 87
1
2 3
99 100