1. 资源管理问题背景
class Investment { ... }; // root class of hierarchy of
// investment types
Investment* createInvestment(); // return ptr to dynamically allocated
// object in the Investment hierarchy;
// the caller must delete it
// (parameters omitted for simplicity)
void f()
{
Investment *pInv = createInvestment(); // call factory function
... // use pInv
delete pInv; // release object
}
- 在使用如
createInvestment
这类返回动态分配对象指针的函数时,若仅依靠手动在合适位置(如函数末尾)调用delete
来释放资源,存