考虑以下类声明:
#include "classB.h"
class A {
private:
B *prop;
public:
A() { /* does stuff to set up B as a pointer to a new B */
setB(const B& bar) { /* store a copy of B */
// how do I manage the old *prop?
*prop = new B(bar);
}
};
应该setB()
如何管理内存分配?我应该删除旧的*prop
吗?如果是这样,我是否取消引用然后delete
?