Multiple Inheritance and enable_shared_from_this
http://www.codeproject.com/Articles/286304/Solution-for-multiple-enable_shared_from_this-in-i
http://stackoverflow.com/questions/5232712/questions-regarding-shared-from-this
1)shared_from_this
enables you to guarantee that if you have one shared_ptr
instance to your object then you can construct another without copying the first, and that these instances will share the reference count. You could achieve this by storing a weak_ptr
as a class member, and setting that value when you first allocate a shared_ptr
to your object.
2) Calling shared_from_this()
requires that there is at least one shared_ptr
instance already pointing to your object. If you use it on an automatic object without a shared_ptr
instance with a custom deleter then you will get bad stuff happening.
3) If you derive from your class then the enable_shared_from_this
functionality will give you a shared_ptr
to the base class (the one that derived from enable_shared_from_this
). You could then use static_pointer_cast
or dynamic_pointer_cast
to cast the result of shared_from_this()
to a pointer to the derived class.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步