正常
Smart Pointers and RAII
Manually managing resources (new/delete, fopen/fclose, lock/unlock) is a nightmare source of bugs for C++ programmers. The RAII (Resource Acquisition Is Initialization) principle tells us to bind resource acquisition to object construction, leave release to destructors, and let scope manage the lifetime for you. In this chapter, we first dive deep into RAII, then master 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 guards handle more complex resource scenarios.
Chapter Contents
Deep Dive into RAII: The Cornerstone of Resource ManagementUnderstanding unique_ptr: Zero-Overhead Smart Pointer with Exclusive OwnershipUnderstanding shared_ptr: Shared Ownership and Reference Countingweak_ptr and Circular ReferencesCustom Deleters and Intrusive Reference Countingscope_guard and defer: Generic Scope Guards