shared-ptr

When is std::weak_ptr useful?

天涯浪子 提交于 2019-11-26 04:29:02
问题 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? 回答1: A good example would be a cache. For recently accessed objects, you want to keep them in memory, so you hold a strong pointer to them. Periodically, you scan the cache and decide which objects have not been accessed recently. You don't need to keep those in memory, so you get rid of the strong pointer. But what if that object is in use

Why do std::shared_ptr<void> work

亡梦爱人 提交于 2019-11-26 03:48:54
问题 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 <memory> #include <iostream> #include <vector> class test { public: test() { std::cout << \"Test created\" << std::endl; } ~test() { std::cout << \"Test destroyed\" << std::endl; } }; int main() { std::cout << \"At begin of main.\\ncreating std::vector<std::shared_ptr<void>>\" << std::endl; std::vector<std::shared_ptr

Why isn&#39;t there a std::shared_ptr<T[]> specialisation?

淺唱寂寞╮ 提交于 2019-11-26 02:38:27
问题 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 available, so it is necessary to to provide a deleter which correctly calls delete[] : void func() { // Usage shared_ptr array (new double [256], [](double* arr) { delete [] arr; } ); .............. } Is this simply an oversight? (in the same way that there

Differences between unique_ptr and shared_ptr [duplicate]

走远了吗. 提交于 2019-11-26 01:48:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: pimpl: shared_ptr or unique_ptr smart pointers (boost) explained Could someone explain differences between shared_ptr and unique_ptr? 回答1: Both of these classes are smart pointers, which means that they automatically (in most cases) will deallocate the object that they point at when that object can no longer be referenced. The difference between the two is how many different pointers of each type can refer to a

Difference in make_shared and normal shared_ptr in C++

南笙酒味 提交于 2019-11-26 01:43:39
问题 std::shared_ptr<Object> p1 = std::make_shared<Object>(\"foo\"); std::shared_ptr<Object> 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 using shared_ptr . Can someone explain me step by step sequence of objects created and operations done by both so that I will be able to understand how make_shared is efficient. I have given one example above for reference. 回答1: The difference is

shared_ptr to an array : should it be used?

半城伤御伤魂 提交于 2019-11-26 00:21:51
问题 Just a small query regarding shared_ptr. Is it a good practice to use shared_ptr pointing to an array? For example, shared_ptr<int> sp(new int[10]); If not, then why not? One reason I am already aware of is one can not increment/decrement the shared_ptr . Hence it can not be used like a normal pointer to an array. 回答1: With C++17 , shared_ptr can be used to manage a dynamically allocated array. The shared_ptr template argument in this case must be T[N] or T[] . So you may write shared_ptr<int

When to use virtual destructors?

浪子不回头ぞ 提交于 2019-11-25 21:35:21
问题 I have a solid understanding of most OO 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 virtual and why? 回答1: Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class: class Base { // some virtual methods }; class Derived : public Base { ~Derived() { // Do some important