enable-shared-from-this

When should we use std::enable_shared_from_this

六月ゝ 毕业季﹏ 提交于 2020-04-05 15:06:49
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std:

When should we use std::enable_shared_from_this

拈花ヽ惹草 提交于 2020-04-05 15:06:03
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std:

boost shared_from_this and multiple inheritance

≡放荡痞女 提交于 2020-01-09 19:47:48
问题 I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from enable_shared_from_this Class B implements another functionality and should inherit from enable_shared_from_this Class D inherits functionalities from A and B ( class D : public A, public B {} ) When using some class B functionality from class D I got an exception ( bad_weak_ptr ) To

boost shared_from_this and multiple inheritance

心已入冬 提交于 2020-01-09 19:47:35
问题 I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from enable_shared_from_this Class B implements another functionality and should inherit from enable_shared_from_this Class D inherits functionalities from A and B ( class D : public A, public B {} ) When using some class B functionality from class D I got an exception ( bad_weak_ptr ) To

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

bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this

↘锁芯ラ 提交于 2019-12-03 19:40:35
问题 I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this: #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> using namespace boost; class A : public enable_shared_from_this<A> { }; class B : public A , public enable_shared_from_this<B> { public: using enable_shared_from_this<B>::shared_from_this; }; int main() { shared_ptr<B> b = shared_ptr<B>(new B());

bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this

谁说我不能喝 提交于 2019-11-30 10:06:59
I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this: #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> using namespace boost; class A : public enable_shared_from_this<A> { }; class B : public A , public enable_shared_from_this<B> { public: using enable_shared_from_this<B>::shared_from_this; }; int main() { shared_ptr<B> b = shared_ptr<B>(new B()); shared_ptr<B> b_ = b->shared_from_this(); return 0; } This compiles but at runtime it gives terminate

Double inheritance of enable_shared_from_this

蹲街弑〆低调 提交于 2019-11-29 18:19:43
问题 I have an object (Z) which derives from two other objects (A and B). A and B both derive from enable_shared_from_this<> , respectively enable_shared_from_this<A> and enable_shared_from_this<B> . Of course I call shared_from_this() on Z. And of course the compiler reports this as ambiguous. My questions are : is it safe to inherit twice from enable_shared_from_this<> or will it create two separated reference counts (bad !) If not safe, how do I solve this ? Note : I've found this other

std::enable_shared_from_this; public vs private

末鹿安然 提交于 2019-11-29 13:28:49
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 of difference because executing a something like this: int main() { auto t(std::make_shared<shared