正常
Smart Pointers and RAII
Managing resources by hand (new/delete, fopen/fclose, lock/unlock) is the source of C++ programmers' nightmares. The RAII principle tells us: bind acquiring a resource to an object's construction, hand releasing it to the destructor, and let the scope manage the lifetime for you. In this chapter we first build a deep understanding of RAII, then set up the "ownership" model—exclusive, shared, and borrowed, each in its rightful place—then walk through the design philosophy and correct usage of unique_ptr, shared_ptr, and weak_ptr one by one, and finally see how custom deleters and scope_guard handle more complex resource scenarios.
Chapter Contents
Deep Dive into RAII: The Cornerstone of Resource ManagementResource Ownership: Exclusive, Shared, and BorrowedDeep Dive into unique_ptr: The Zero-Overhead Smart Pointer with Exclusive OwnershipDeep Dive into shared_ptr: Shared Ownership and Reference Countingweak_ptr and Circular ReferencesCustom Deleters and Intrusive Reference Countingscope_guard and defer: A General-Purpose Scope GuardThe PIMPL Idiom: unique_ptr, shared_ptr, and Incomplete Types