Home >Backend Development >C++ >Can `shared_ptr` Function Correctly Without Virtual Destructors in Polymorphic Classes?

Can `shared_ptr` Function Correctly Without Virtual Destructors in Polymorphic Classes?

Susan Sarandon
Susan SarandonOriginal
2024-12-08 13:50:10718browse

Can `shared_ptr` Function Correctly Without Virtual Destructors in Polymorphic Classes?

Implementation of shared_ptr without Virtual Destructors

In our debate, we questioned the necessity of requiring polymorphic classes to have virtual destructors for shared_ptr implementation. The argument presented claimed that shared_ptr could function correctly without this requirement.

Let's delved deeper into the implementation of shared_ptr to determine the validity of this claim.

Type Erasure and Templated Constructor

shared_ptr utilizes type erasure to store a deleter within the same memory block as its reference counters. The type of this deleter is not part of the shared_ptr type. By employing a templated constructor, shared_ptr can accommodate various deleter types based on the specific class being managed.

The following constructor can be defined to handle this:

template<class T>
class shared_ptr
{
public:
   ...
   template<class Y>
   explicit shared_ptr(Y* p);
   ...
};

When using this constructor with the classes Base and Derived, the appropriate deleter can be created and stored. This deleter will be invoked when the reference counter drops to zero, ensuring proper object disposal.

C 11 Standard Requirements

The C 11 standard explicitly requires this behavior for the templated constructor:

"Effects: Constructs a shared_ptr object that owns the pointer p."

This confirms that shared_ptr assumes ownership of the passed pointer and is responsible for its destruction.

Destructor Functionality

The destructor behavior is defined as follows:

"Effects: If this owns an object p and a deleter d, d(p) is called. Otherwise, if this owns a pointer p, and delete p is called."

This indicates that if shared_ptr directly manages a pointer (i.e., without an explicit deleter), the default destructor delete is utilized to dispose of the object.

Conclusion

Based on these guidelines, it is evident that shared_ptr can be implemented without requiring polymorphic classes to have virtual destructors. The aforementioned techniques, such as type erasure and the templated constructor, enable shared_ptr to effectively manage objects of any type, including those that do not support polymorphism.

The above is the detailed content of Can `shared_ptr` Function Correctly Without Virtual Destructors in Polymorphic Classes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn