In a recent debate, Daniel Lidström asserted that a shared_ptr
Implementing Shared_Ptr Without Virtual Destructors
The key to this implementation lies in type erasure. Shared_ptr stores more than just reference counters; it includes a deleter (stored as part of the data structure) that handles object destruction. This deleter can be of any type, allowing for customization beyond the scope of T (the template type of shared_ptr).
Customization for Derived Classes
When creating a shared_ptr
Example Implementation
template<class T> class shared_ptr { public: ... template<class Y> explicit shared_ptr(Y* p); ... };
The above constructor allows the creation of a shared_ptr
C 11 Standard Requirements
The C 11 standard explicitly requires this behavior:
The above is the detailed content of Can `shared_ptr` Manage `Derived` Objects Without a Virtual Destructor in `Base`?. For more information, please follow other related articles on the PHP Chinese website!