Available C Smart Pointer Implementations and Their Comparisons
C 03
-
std::auto_ptr: Deprecated in future C standards. Limited garbage collection, ownership complications, and inability to use in STL containers.
-
std::auto_ptr_ref: Not a true smart pointer; used in conjunction with std::auto_ptr to allow copying and assignment.
C 11
-
std::unique_ptr: Replaces std::auto_ptr. Corrects weaknesses of std::auto_ptr, allowing work with arrays, lvalue protection, and use in STL containers.
-
std::shared_ptr: Reference-counted smart pointer. Supports aliasing and pointer arithmetic.
-
std::weak_ptr: Non-owning reference to an object owned by a std::shared_ptr. Prevents indefinite cyclic reference counts.
Boost
-
boost::shared_ptr: Popular shared reference-counted smart pointer. High performance and versatility.
-
boost::weak_ptr: Non-owning reference to a boost::shared_ptr.
-
boost::scoped_ptr: Simple smart pointer alternative to boost::shared_ptr with less overhead.
-
boost::intrusive_ptr: Custom smart pointer for creating your own smart pointer-compatible classes.
-
boost::shared_array: Shared smart pointer for arrays.
-
boost::scoped_array: Non-copyable smart pointer for arrays.
Qt
-
QPointer: Weak smart pointer for QObject and derived classes. Deprecated.
-
QSharedDataPointer: Strong smart pointer comparable to boost::intrusive_ptr, with thread safety.
-
QExplicitlySharedDataPointer: Version 2.0 of QSharedDataPointer with enhanced control over detachment.
-
QSharedPointer: Atomic reference-counting, thread-safe, sharable smart pointer.
-
QWeakPointer: Non-owning reference to a QSharedPointer.
-
QScopedPointer: Single-owner smart pointer based on boost::scoped_ptr, suitable for exception-safe code and compatibility.
The above is the detailed content of Which C Smart Pointer Should I Choose?. For more information, please follow other related articles on the PHP Chinese website!