A smart pointer is a C++ library type that manages an object pointer and helps automate ownership and storage cleanup. It reduces the need to pair every allocation with a manual delete. section 1.1 section 6.2.1
Smart pointers become especially useful when a program stores objects of different derived types together. A vector of smart pointers can refer to objects sharing a common base type while preserving their differing behavior through polymorphism. section 6.0 section 6.2.1
The practical advantage appears when managing polymorphic objects:
- Raw pointers can support the required virtual-method behavior, but their objects must be deleted by hand. section 6.2.1
- Manual cleanup becomes harder when references might go out of scope too soon or an exception interrupts the program. section 6.2.1
- Smart pointers address these cleanup challenges while allowing different blob types to compete through a shared interface. section 6.0 section 6.2.1
A polymorphic blob race
A race contains stepper blobs and randomly moving blobs. Because both derive from a common Blob type, a vector of smart pointers can hold them together; virtual methods then make each blob move according to its own implementation. The smart pointers remove the need to delete every blob pointer manually when the race ends or fails with an exception. section 6.0 section 6.2.1




