shared-ptr

Using shared_ptr for unique ownership (kind of) - is this good practice?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:14:51
问题 this is quite hard to explain but I'll try my best. So, I have a RenderComponent, EventManager and RenderSystem. In my RenderComponents constructor, I raise a renderComponentCreated event which the RenderSystem subscribes to. Using an event args object, I pass a RenderNode as data which contains the information the renderSystem needs to draw the thing (drawable, position and type). So far so good. Now, when the renderComponent is deleted, I want the RenderNode to be removed from the

Create a boost::shared_ptr to an existing variable

五迷三道 提交于 2019-11-28 05:50:09
I have an existing variable, e.g. int a = 3; How can I now create a boost::shared_ptr to a ? For example: boost::shared_ptr< int > a_ptr = &a; // this doesn't work although you should put the variable into a managed pointer on it's creation to do it from an existing pointer. int *a=new int; boost::shared_ptr<int> a_ptr(a); That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN If for some reason a function takes shared_ptr and you only have a stack varaible you are better off doing this: int a=9; boost::shared_ptr<int> a_ptr=boost::make

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

六月ゝ 毕业季﹏ 提交于 2019-11-28 05:45:35
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 to place the ref count in a separate allocation instead of in the same allocation as the T itself?) Is

Equality-compare std::weak_ptr

回眸只為那壹抹淺笑 提交于 2019-11-28 04:32:16
I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality. What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same. The comparison should yield negative results not just if the addresses don't match, but also if the underlying object was deleted and then reconstructed with the same address by chance. So basically, I want this assertion to hold even if the allocator reserves the same address: auto s1 = std::make_shared<int>(43); std::weak_ptr<int> w1(s1); s1.reset(); auto s2 = std::make_shared<int>(41); std::weak

Cohabitation of boost::shared_ptr and std::shared_ptr

我怕爱的太早我们不能终老 提交于 2019-11-28 03:08:39
I want to use boost::log at some point, but I cannot pass a std::shared_ptr as a parameter, because the compiler (VS2010) cannot convert it into a boost::shared_ptr . I don't really like the fact that they are aliens to one another. Is there a safe and transparent way to convert one into the another, so as they don't stumble over each other? I don't think it is duplicate of this question that states both are the same. You could do it like this: template<typename T> boost::shared_ptr<T> make_shared_ptr(std::shared_ptr<T>& ptr) { return boost::shared_ptr<T>(ptr.get(), [ptr](T*) mutable {ptr

Differences between different flavours of shared_ptr

烂漫一生 提交于 2019-11-28 02:37:55
问题 Are there any differences between boost::shared_ptr, std::tr1::shared_ptr and the upcoming (in C++0x ) std::shared_ptr ? Will porting from one to another have any overhead or are they basically the same? 回答1: According to the Boost website, the boost::shared_ptr ... ...conforms to the TR1 specification, with the only exception that it resides in namespace boost instead of std::tr1 . According to the Wikipedia C++0x page The TR1 implementation lacked certain pointer features such as aliasing

Using std::shared_ptr<void> to point to anything

泄露秘密 提交于 2019-11-28 01:12:46
问题 I'm using a std::shared_ptr<void> in my application to make a smart pointer which can point to many different types of data structures like structs, vectors, matrices... basically anything. What I'm trying to do is map some names to their data structures. I'm performing the mapping using a hashtable: std::unordered_map<std::string, std::shared_ptr<void>> Can I cast the std::shared_ptr<void> returned by find() back to std::shared_ptr<my_type> ? If so, how? More importantly, is this good

Why shared_from_this can't be used in constructor from technical standpoint?

久未见 提交于 2019-11-28 00:38:51
In the book The C++ Standard Library at page 91 I have read this about shared_from_this() : The problem is that shared_ptr stores itself in a private member of Person ’s base class, enable_shared_from_this<> , at the end of the construction of the Person. The relevant code snippet from the book is: class Person : public std::enable_shared_from_this<Person> { ... }; I don't understand two things here: who is this shared_ptr which stores itself? how he can store itself anywhere at the end of the construction of Person ? I think construction of Person ends up with the last statement of its

How to intentionally delete a boost::shared_ptr?

筅森魡賤 提交于 2019-11-27 20:38:02
问题 I have many boost::shared_ptr<MyClass> objects, and at some point I intentionally want to delete some of them to free some memory. (I know at that point that I will never need the pointed-to MyClass objects anymore.) How can I do that? I guess you can't just call delete() with the raw pointer that I get with get() . I've seen a function get_deleter(shared_ptr<T> const & p) in boost::shared_ptr , but I'm not sure how to use it, and also it says experimental right next to it. (I think I have

Use of enable_shared_from_this with multiple inheritance

与世无争的帅哥 提交于 2019-11-27 20:25:35
问题 BI am using enable_shared_from_this in my code, and I am not sure if its usage is correct. This is the code: class A: public std::enable_shared_from_this<A> { public: void foo1() { auto ptr = shared_from_this(); } }; class B:public std::enable_shared_from_this<B> { public: void foo2() { auto ptr = shared_from_this(); } }; class C:public std::enable_shared_from_this<C> { public: void foo3() { auto ptr = shared_from_this(); } }; class D: public A, public B, public C { public: void foo() { auto