Item 15 - Provide access to raw resources in resource-managing classes.
std::shared_ptr<Investment> pInv (createInvestment());int daysHeld(const Investment* pi); // return number of days investment has been heldint days = daysHeld(pInv); // error!int days = daysHeld(pInv.get()); // fine, passes the raw pointer in pInv to daysHeldclass 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*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