smart-pointers

Return Type Covariance with Smart Pointers

非 Y 不嫁゛ 提交于 2019-11-27 03:57:04
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 standard says that the return type of an overriding member may change only from reference(or pointer) to

How to check memory allocation failures with new operator?

落爺英雄遲暮 提交于 2019-11-27 03:45:08
问题 Just recently I switched the language of my project to use C++ from C. With C, I used malloc and after that I check if malloc was successful but with C++, I use 'new' to allocate memory and I would like to know how you would normally check the memory allocation failure. From my google search, I saw nothrow like the following. char *buf = new (nothrow)char[10]; I also saw the following. try{} catch(bad_alloc&) {} But what about the following? I am using some of chrome library routines to use

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

走远了吗. 提交于 2019-11-27 03:17:50
This question already has an answer here: Difference in make_shared and normal shared_ptr in C++ 7 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. Mike Seymour Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or '=shared_ptr<int>(new int)' if you insist auto p = std::make_shared<int>(); // or 'std::shared_ptr<int> p' if

Where is shared_ptr?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 03:05:29
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 <memory> is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by telling exactly where to find it? Thanks for letting me vent my frustrations! EDIT: I see my title has been changed. Sorry about that. So... it was also because it was not clear to me that shared_ptr is "C++ version dependant" --> that's why I did not state my

When to use shared_ptr and when to use raw pointers?

為{幸葍}努か 提交于 2019-11-27 02:42:21
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 is explicit transfer or sharing of ownership, which I think is quite rare outside of cases where a

Use of observer_ptr

那年仲夏 提交于 2019-11-27 01:45:13
问题 What exactly is the point of the construct std::observer_ptr in the library fundamentals technical specification V2? It seems to me that all it does is wrap a bare T* , which seems like a superfluous step if it adds no dynamic memory safety. In all of my code I use std::unique_ptr where I need to take explicit ownership of an object and std::shared_ptr where I can share ownership of an object. This works very well and prevents accidental dereferencing of an already destroyed object. std:

C++0x unique_ptr replaces scoped_ptr taking ownership?

一世执手 提交于 2019-11-27 01:39:50
问题 I used to write code like this: class P {}; class Q: public P {}; class A { // takes ownership A(P* p): p_(p) {} scoped_ptr<P> p_; }; A a(new Q); With C++0x, should I rewrite class A as: class A { // takes ownership A(unique_ptr<P>&& p): p_(p) {} unique_ptr<P> p_; }; 回答1: I've upvoted comonad's answer, but with a caveat: Whenever you want to explicitely disallow move semantics, use a scoped_ptr const unique_ptr . I have not come across any use cases where a const std::unique_ptr is inferior

How to break shared_ptr cyclic reference using weak_ptr

佐手、 提交于 2019-11-27 01:23:17
问题 I have read that weak_pointers can be used to break cyclic references. Consider the following example of a cyclic reference struct A { boost::shared_ptr<A> shrd_ptr; }; boost::shared_ptr<A> ptr_A(boost::make_shared<A>()); boost::shared_ptr<A> ptr_b(boost::make_shared<A>()); ptr_A->shrd_ptr = ptr_b; ptr_b->shrd_ptr = ptr_A; Now above is a case of cyclic reference and I wanted to know how I can break the cyclic reference above by using weak_ptr ? Update : Based on suggestion received I came up

How can I use covariant return types with smart pointers?

一曲冷凌霜 提交于 2019-11-27 01:23:08
I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual studio it raises C2555: overriding virtual function return type differs and is not covariant If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to covariant return types in C++). I can see the problem is because boost::shared_ptr of Ret1 is

Can Google Mock a method with a smart pointer return type?

牧云@^-^@ 提交于 2019-11-27 00:18:19
I have a factory that returns a smart pointer. Regardless of what smart pointer I use, I can't get Google Mock to mock the factory method. The mock object is the implementation of a pure abstract interface where all methods are virtual. I have a prototype: MOCK_METHOD0(Create, std::unique_ptr<IMyObjectThing>()); And I get: "...gmock/gmock-spec-builders.h(1314): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'" The type pointed to in the smart pointer is defined. And I get it's trying to access one of the constructors