正文
1.不要手动释放从函数返回的堆资源
假设你正在处理一个模拟Investment的程序库,不同的Investmetn类型从Investment基类继承而来,
1 class Investment { ... }; // root class of hierarchy of2 3 // investment types进一步假设这个程序库通过一个工厂函数(Item 7)来给我们提供特定Investment对象:
1 Investment* createInvestment(); // return ptr to dynamically allocated2 3 // object in the Investment hierarchy;4 5 // the caller must delete it6 7 // (parameters omitted for simplicity)
正如注释所表述的,当createInvesment返回的对象不再被使用时,调用者有责任将此对象释放掉。我们用函数f来履行这个职责:


