Effective C++ by Scott Meyers (Short Summary)
  • Introduction
  • Chapter 1 - Accustoming Yourself to C++
    • Item 1 - View C++ as a federation of languages.
    • Item 2 - Prefer consts, enums, and inlines to #defines.
    • Item 3 - Use const whenever possible.
    • Item 4 - Make sure that objects are initialized before they’re used.
  • Chapter 2 - Constructors, Destructors, and Assignment Operators
    • Item 5 - Know what functions C++ silently writes a calls.
    • Item 6 - Explicitly disallow the use of compiler-generated functions you do not want.
    • Item 7 - Declare destructors virtual in polymorphic base classes.
    • Item 8 - Prevent exceptions from leaving destructors.
    • Item 9 - Never call virtual functions during construction or destruction.
    • Item 10 - Have assignment operators return a reference to *this.
    • Item 11 - Handle assignment to self in operator=.
    • Item 12 - Copy all parts of an object.
  • Chapter 3 - Resource Management
    • Item 13 - Use objects to manage resources.
    • Item 14 - Think carefully about copying behavior in resource-managing classes.
    • Item 15 - Provide access to raw resources in resource-managing classes.
    • Item 16 - Use the same form in corresponding uses of new and delete.
    • Item 17 - Store newed objects in smart pointers in standalone statements.
  • Chapter 4 - Designs and Declarations
    • Item 18 - Make interfaces easy to use correctly and hard to use incorrectly.
    • Item 19 - Treat class design as type design.
    • Item 20 - Prefer pass-by-reference-to-const to pass-by-value.
    • Item 21 - Don't try to return a reference when you must return an object.
    • Item 22 - Declare data members private.
    • Item 23 - Prefer non-member non-friend functions to member functions.
    • Item 24 - Declare non-member functions when type conversions should apply to all parameters.
    • Item 25 - Consider support for a non-throwing swap.
Powered by GitBook
On this page
  1. Chapter 3 - Resource Management

Item 15 - Provide access to raw resources in resource-managing classes.

PreviousItem 14 - Think carefully about copying behavior in resource-managing classes.NextItem 16 - Use the same form in corresponding uses of new and delete.

Last updated 3 years ago

introduces the idea of using smart pointers like auto_ptr or shared_ptr to hold the result of a call to a factory function like createInvestment:

std::shared_ptr<Investment> pInv (createInvestment());

Suppose that a function you'd like to use when working with Investment objects is this:

int daysHeld(const Investment* pi);    // return number of days investment has been held

You'd like to call it like this

int days = daysHeld(pInv);    // error!

but the code won't compile: daysHeld wants a raw Investment* pointer, but you're passing an object of type shared_ptr<Investment>. You need a way to convert an object of the RAII class (in this case shared_ptr) into the raw resource it contains (e.g., the underlying Investment*). There are two general ways to do it: explicit conversion and implicit conversion. shared_ptr and auto_ptr both offer a get member function to perform an explicit conversion, i.e., to return (a copy of) the raw pointer inside the smart pointer object:

int days = daysHeld(pInv.get());        // fine, passes the raw pointer in pInv to daysHeld

Like virtually all smart pointer classes shared_ptr and auto_ptr also overload the pointer dereferencing operators (-> and *), and this allows implicit conversion to the underlying raw pointers:

class Investment {            // root class for a hierarchy of investment types
public:
    bool isTaxFree() const;
    ...
};
Investment* createInvestment();                    // factory function

shared_ptr<Investment> pi1(createInvestment());    // shared_ptr manage a resource
bool taxable1 = !(pi1->isTaxFree());               // access resource via operator->
...
auto_ptr<Investment> pi2(createInvestment()):      // auto_ptr manage a resource
bool taxable2 = !((*pi2).isTaxFree());             // access resource via operator*

Things to Remember

  • APIs often require access to raw resource, so each RAII class should offer a way to get to the resource it manages.

  • Access may be via explicit conversion or implicit conversion. In general, explicit conversion is safer, but implicit conversion is more convenient for clients.

Item 13