shared-ptr

Is there a way to cast shared_ptr<void> to shared_ptr<T>?

余生长醉 提交于 2019-12-18 14:14:31
问题 I want to keep the smart behavior of std::shared_ptr . So is there a way to cast a shared void pointer to another type while without confusing the reference counting? I can't get the raw pointer and create a new shared pointer from it. 回答1: You can use the pointer casts from rob mayoff's answer; but be careful. It is easy to unintentionally trigger undefined behavior here: struct MyClass {}; void* rawPtr = new MyClass; shared_ptr<void> exampleVoid(rawPtr); // Undefined behavior; // calls

How to handle 'this' pointer in constructor?

点点圈 提交于 2019-12-18 10:58:21
问题 I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw pointers. So the child would have code such as shared_ptr<Parent> , and boost provides the shared_from_this() method which the parent can give to the child. My problem is that shared_from_this() cannot be used in a constructor, which isn't really a crime

How to pass deleter to make_shared?

允我心安 提交于 2019-12-18 10:48:08
问题 Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int>

std::enable_shared_from_this; public vs private

前提是你 提交于 2019-12-18 07:33:43
问题 I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot

std::enable_shared_from_this; public vs private

眉间皱痕 提交于 2019-12-18 07:33:20
问题 I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot

Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr

独自空忆成欢 提交于 2019-12-18 06:57:48
问题 I have read through many questions on SO on custom deleter for shared_ptr and unique_ptr , and the difference between the two. But, I still haven't found any clear answer to this question: How can one best go about creating a type that acts as a shared_ptr with a custom deleter, similar to how unique_ptr has the deleter as part of the type definition? For unique_ptr usage, I use a deleter class, that handles deletion of individual types (limiting it to just two types, for brevity): struct SDL

How does shared_ptr work in if condition

可紊 提交于 2019-12-18 05:51:00
问题 In C++, I can write something like: shared_ptr<A> a_sp = someFunctionReturningSharedPtr(); if (a_sp) { cout << a_sp->someData << endl; } else { cout << "Shared Pointer is NULL << endl; } Why does if (a_sp) check work correctly? a_sp is not a boolean, but how is it checked for true or false? How does the if condition know to check the result of a_sp.get() function? Or if it does not, how is the NULL ity of the a_sp checked? Is there some function in shared_ptr defined that converts it to

Why std::shared_ptr calls destructors from base and derived classes, where delete calls only destructor from base class? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-18 02:42:34
问题 This question already has answers here : shared_ptr magic :) (3 answers) Closed 5 years ago . Why when using std::shared_ptr deallocation calls destructors from both base and derived classes when second example calls only destructor from base class? class Base { public: ~Base() { std::cout << "Base destructor" << std::endl; } }; class Derived : public Base { public: ~Derived() { std::cout << "Derived destructor" << std::endl; } }; void virtual_destructor() { { std::cout << "------------------

Why std::shared_ptr calls destructors from base and derived classes, where delete calls only destructor from base class? [duplicate]

限于喜欢 提交于 2019-12-18 02:42:10
问题 This question already has answers here : shared_ptr magic :) (3 answers) Closed 5 years ago . Why when using std::shared_ptr deallocation calls destructors from both base and derived classes when second example calls only destructor from base class? class Base { public: ~Base() { std::cout << "Base destructor" << std::endl; } }; class Derived : public Base { public: ~Derived() { std::cout << "Derived destructor" << std::endl; } }; void virtual_destructor() { { std::cout << "------------------

What (not) to do in a constructor

◇◆丶佛笑我妖孽 提交于 2019-12-17 21:45:42
问题 I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not. Should I only use it for attribute initializations, calling parent constructors etc.? Or might I even put more complex functions into them like reading and parsing configuration data, setting up external libraries a.s.o. Or should I write special functions for this? Resp. init() / cleanup() ? What are the PRO's and CON's here? I figured out yet that for