How Does the Smart_ptr Library Improve Your Programs?


  • Automatic lifetime management of objects with shared_ptr makes shared ownership of resources effective and safe.

  • Safe observation of shared resources through weak_ptr avoids dangling pointers.

  • Scoped resources using scoped_ptr and scoped_array make the code easier to write and maintain, and helps in writing exception-safe code.

Smart pointers solve the problem of managing the lifetime of resources (typically dynamically allocated objects[1]). Smart pointers come in different flavors. Most share one key featureautomatic resource management. This feature is manifested in different ways, such as lifetime control over dynamically allocated objects, and acquisition and release of resources (files, network connections). The Boost smart pointers primarily cover the first casethey store pointers to dynamically allocated objects, and delete those objects at the right time. You might wonder why these smart pointers don't do more. Couldn't they just as easily cover all types of resource management? Well, they could (and to some extent they do), but not without a price. General solutions often imply increased complexity, and with the Boost smart pointers, usability is of even higher priority than flexibility. However, through the support for custom deleters, Boost's arguably smartest smart pointer (boost::shared_ptr) supports resources that need other destruction code than delete. The five smart pointer types in Boost.Smart_ptr are tailor-made to fit the most common needs that arise in everyday programming.

[1] Just about any type of resource can be handled by a generic smart pointer type.



    Beyond the C++ Standard Library(c) An Introduction to Boost
    Beyond the C++ Standard Library: An Introduction to Boost
    ISBN: 0321133544
    EAN: 2147483647
    Year: 2006
    Pages: 125

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net