smart-pointers

shared_ptr magic :)

余生颓废 提交于 2019-11-26 11:05:32
Mr. Lidström and I had an argument :) Mr. Lidström's claim is that a construct shared_ptr<Base> p(new Derived); doesn't require Base to have a virtual destructor: Armen Tsirunyan : "Really? Will the shared_ptr clean up correctly? Could you please in this case demonstrate how that effect could be implemented?" Daniel Lidström : "The shared_ptr uses its own destructor to delete the Concrete instance. This is known as RAII within the C++ community. My advice is that you learn all you can about RAII. It will make your C++ coding so much easier when you use RAII in all situations." Armen Tsirunyan

Return Type Covariance with Smart Pointers

本秂侑毒 提交于 2019-11-26 10:49:24
问题 In C++ we can do this: struct Base { virtual Base* Clone() const { ... } virtual ~Base(){} }; struct Derived : Base { virtual Derived* Clone() const {...} //overrides Base::Clone }; However, the following won\'t do the same trick: struct Base { virtual shared_ptr<Base> Clone() const { ... } virtual ~Base(){} }; struct Derived : Base { virtual shared_ptr<Derived> Clone() const {...} //hides Base::Clone }; In this example Derived::Clone hides Base::Clone rather than overrides it, because the

std::shared_ptr initialization: make_shared<Foo>() vs shared_ptr<T>(new Foo) [duplicate]

你离开我真会死。 提交于 2019-11-26 10:21:49
问题 This question already has an answer here: Difference in make_shared and normal shared_ptr in C++ 8 answers What\'s the difference between: std::shared_ptr<int> p = std::shared_ptr<int>( new int ); and std::shared_ptr<int> p = std::make_shared< int >(); ? Which one should I prefer and why? P. S. Pretty sure this must have been answered already, but I can\'t find a similar question. 回答1: Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or '=shared_ptr

Differences between std::make_unique and std::unique_ptr with new

百般思念 提交于 2019-11-26 10:21:00
Does std::make_unique have any efficiency benefits like std::make_shared ? Compared to manually constructing std::unique_ptr : std::make_unique<int>(1); // vs std::unique_ptr<int>(new int(1)); The motivation behind make_unique is primarily two-fold: make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries. foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique_ptr<U>(new U())); // unsafe* The addition of make_unique finally means we can tell people to 'never' use new

When to use shared_ptr and when to use raw pointers?

夙愿已清 提交于 2019-11-26 10:09:59
问题 class B; class A { public: A () : m_b(new B()) { } shared_ptr<B> GimmeB () { return m_b; } private: shared_ptr<B> m_b; }; Let\'s say B is a class that semantically should not exist outside of the lifetime of A, i.e., it makes absolutely no sense for B to exist by itself. Should GimmeB return a shared_ptr<B> or a B* ? In general, is it good practice to completely avoid using raw pointers in C++ code, in lieu of smart pointers? I am of the opinion that shared_ptr should only be used when there

std::auto_ptr to std::unique_ptr

非 Y 不嫁゛ 提交于 2019-11-26 09:05:13
问题 With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr . Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not apparent from reading the documentation? Also if it is a direct replacement, why give it a new name rather than just improve the std::auto_ptr ? 回答1: You cannot do

Is there a non-atomic equivalent of std::shared_ptr? And why isn&#39;t there one in <memory>?

☆樱花仙子☆ 提交于 2019-11-26 08:59:45
问题 This is a bit of a two part question, all about the atomicity of std::shared_ptr : 1. As far as I can tell, std::shared_ptr is the only smart pointer in <memory> that\'s atomic. I\'m wondering if there is a non-atomic version of std::shared_ptr available (I can\'t see anything in <memory> , so I\'m also open to suggestions outside of the standard, like those in Boost). I know boost::shared_ptr is also atomic (if BOOST_SP_DISABLE_THREADS isn\'t defined), but maybe there\'s another alternative?

How much is the overhead of smart pointers compared to normal pointers in C++?

女生的网名这么多〃 提交于 2019-11-26 07:18:36
问题 How much is the overhead of smart pointers compared to normal pointers in C++11? In other words, is my code going to be slower if I use smart pointers, and if so, how much slower? Specifically, I\'m asking about the C++11 std::shared_ptr and std::unique_ptr . Obviously, the stuff pushed down the stack is going to be larger (at least I think so), because a smart pointer also needs to store its internal state (reference count, etc), the question really is, how much is this going to affect my

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

“Downcasting” unique_ptr<Base> to unique_ptr<Derived>

别说谁变了你拦得住时间么 提交于 2019-11-26 03:38:02
问题 I have a series of factories that return unique_ptr<Base> . Under the hood, though, they are providing pointers to various derived types, i.e unique_ptr<Derived> , unique_ptr<DerivedA> , unique_ptr<DerivedB> etc. Given DerivedA : Derived and Derived : Base we\'d have: unique_ptr<Base> DerivedAFactory() { return unique_ptr<Base>(new DerivedA); } What I need to do is to \"cast\" the pointer from the returned unique_ptr<Base> to some derived level (not necessarily the original internal one). To