[英]will following code cause resource leak?
class Base {
public:
Base(int i1, int i2): ip1{new int(i1)}{
try {
ip2 = new int(i2);
} catch (std::bad_alloc& ){
delete ip1;
}
}
~Base() { delete ip1; delete ip2; }
private:
int *ip1, *ip2;
};
class Derived: public Base {
public:
Derived(int i1, int i2, int i3): Base(i1, i2), ip3(new int(i3)) {}
~Derived() { delete ip3; }
private:
int *ip3;
};
我不在派生构造函数中使用try-catch 。 我想知道ip3(new int(i3))是否抛出异常,是否会删除ip1和ip2分配的内存?
是的,但是通过对所有这些指针使用unique_ptr
和make_unique
,可以使同一件事变得容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.