Home >Backend Development >C++ >What C Smart Pointer Implementations Exist and How Do They Differ?

What C Smart Pointer Implementations Exist and How Do They Differ?

Barbara Streisand
Barbara StreisandOriginal
2024-11-30 10:46:12623browse

What C   Smart Pointer Implementations Exist and How Do They Differ?

What C Smart Pointer Implementations are available?

C 03

  • std::auto_ptr: Limited garbage collection, cannot hold array allocated objects,不支持拷贝,已废弃。
  • std::auto_ptr_ref: Not a smart pointer, used with std::auto_ptr for copying and assignment.

C 11

  • std::unique_ptr: Replaces std::auto_ptr, improved performance, works with arrays, can be used in STL containers.
  • std::shared_ptr: Reference-counted smart pointer, can be shared by multiple owners, thread-safe.
  • std::weak_ptr: Reference to an object owned by std::shared_ptr, does not prevent deletion.

Boost

  • boost::shared_ptr: Standard-compliant shared-ptr, easy to use, high overhead in some cases.
  • boost::weak_ptr: Standard-compliant weak_ptr, allows non-owning references to boost::shared_ptr.
  • boost::scoped_ptr: Similar to std::unique_ptr, less overhead than boost::shared_ptr, cannot be used in STL containers.
  • boost::intrusive_ptr: Customizable smart pointer for creating your own smart pointer-compatible classes.
  • boost::shared_array: Shared_ptr for arrays, supports dynamic array allocation and deletion.
  • boost::scoped_array: Scoped_ptr for arrays, non-copyable, cannot be used in STL containers.

Qt

  • QPointer: Weak smart pointer for QObject and derived classes, does not provide a strong pointer.
  • QSharedDataPointer: Strong smart pointer, requires manual reference counting, can subclass QSharedData.
  • QExplicitlySharedDataPointer: Similar to QSharedDataPointer, provides more control over detaching.
  • QSharedPointer: Atomic reference counting, thread-safe, customizable deletes, high overhead.
  • QWeakPointer: Weak smart pointer, used with QSharedPointer to prevent circular references.
  • QScopedPointer: Single-owner smart pointer, less overhead than QSharedPointer.

The above is the detailed content of What C Smart Pointer Implementations Exist and How Do They Differ?. 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