shared-ptr

Create shared_ptr to stack object

∥☆過路亽.° 提交于 2019-12-17 19:35:13
问题 In my method a Player object is created like: Player player(fullName,age); My teacher gave us a piece of code with a constructor that takes a shared_ptr to a player object. //constructor of the class SomeClass(const std::shared_ptr<Socket> client, std::shared_ptr<Player> player) Lets say we want to call the constructor of SomeClass and pass the player object we created on stack. Is it ever safe/possible/good to create a shared_ptr from a stack object? To make the question more understandable

intrusive_ptr in c++11

我的未来我决定 提交于 2019-12-17 18:13:19
问题 Does C++11 have something equivalent to boost::intrusive_ptr ? My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use std::shared_ptr because I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something like boost::intrusive_ptr . 回答1: Does c++11 have something equivalent to boost::intrusive_ptr? No. It does

is it better to use shared_ptr.reset or operator =?

假如想象 提交于 2019-12-17 17:56:25
问题 I'm trying to wrap my head around the new idioms for C++11. It seems that with shared_ptr at least, there is a substantive difference between using new T() and make_shared<T>() . But what of resetting a shared pointer to point to a new instance of something. Previously, I would typically use reset(new T()) member. However, doesn't this suffer from the same problem as not using make_shared() in the first place? (i.e. it doesn't allow make_shared to allocate the object, therefore it is forced

Error: expected type-specifier before 'ClassName'

走远了吗. 提交于 2019-12-17 17:54:20
问题 shared_ptr<Shape> circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr<Shape> rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above won't compile. For Whatever reason, when I try to create an instance of Rect2f (which DOES inherit from the Shape class specified the shared_ptr template argument, just like Circle ), I get the following errors : error: expected type-specifier before 'Rect2f' error: expected ')' before 'Rect2f'

Using shared_ptr in dll-interfaces

[亡魂溺海] 提交于 2019-12-17 10:37:36
问题 I have an abstract class in my dll. class IBase { protected: virtual ~IBase() = 0; public: virtual void f() = 0; }; I want to get IBase in my exe-file which loads dll. First way is to create following function IBase * CreateInterface(); and to add the virtual function Release() in IBase . Second way is to create another function boost::shared_ptr<IBase> CreateInterface(); and no Release() function is needed. Questions. 1) Is it true that the destructor and memory deallocation is called in the

Detach a pointer from a shared_ptr? [duplicate]

女生的网名这么多〃 提交于 2019-12-17 09:47:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to release pointer from boost::shared_ptr? A function of my interface returns a pointer to an object. The user is supposed to take ownership of that object. I do not want to return a Boost.shared_ptr, because I do not want to force clients to use boost. Internally however, I would like to store the pointer in a shared_ptr to prevent memory leaks in case of exceptions etc. There seems to be no way to detach a

std::shared_ptr thread safety explained

杀马特。学长 韩版系。学妹 提交于 2019-12-17 05:38:18
问题 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 issue - standard guarantees that only one thread (holding last reference) will call delete on shared object, right? shared_ptr does not guarantee any thread safety for object stored in it? EDIT: Pseudo code: // Thread I shared_ptr<A> a (new A (1)); //

std::shared_ptr thread safety

一笑奈何 提交于 2019-12-17 04:15:19
问题 I've read that "Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." (MSDN: Thread Safety in the Standard C++ Library) Does that mean that changing shared_ptr object is safe ? For an instance, is the next code considered safe: shared_ptr<myClass> global = make_shared<myClass>(); ... //In thread 1 shared_ptr<myClass> private = global; ... //In thread 2 global = make_shared<myClass>(); ... Can I be sure in that

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

喜你入骨 提交于 2019-12-16 19:55:03
问题 I have this code that doesn't work, but I think the intent is clear: testmakeshared.cpp #include <memory> class A { public: static ::std::shared_ptr<A> create() { return ::std::make_shared<A>(); } protected: A() {} A(const A &) = delete; const A &operator =(const A &) = delete; }; ::std::shared_ptr<A> foo() { return A::create(); } But I get this error when I compile it: g++ -std=c++0x -march=native -mtune=native -O3 -Wall testmakeshared.cpp In file included from /usr/lib/gcc/x86_64-redhat

How to use shared_ptr to supply mock object from outside?

不想你离开。 提交于 2019-12-14 02:33:54
问题 I want to test an app Thud , which will use a resource Foo . It will not own a concrete object but will have a pointer to a abstract resource interface ( IFoo here). In production I will supply it with the actual resource implementation ( FooImpl ) whereas for unit test I want to send a pointer to a mock resource. How should I do this ? I've tried to write the least code just to get to point, class IFoo { public: virtual bool Foo(bool) = 0; }; class FooImpl : public IFoo { public: bool Foo