首先我修复了你的代码,因为它是一个 C++ 问题,所以它必须写成 C++。构造函数可能会因 bad_alloc 以外的异常而失败。
您的选择在那里:
不要存储指针而是存储对象。这些是自动构建的(或通过初始化列表),如果创建,yes 将自动清理。这可能会更好,但意味着它们需要完全定义,即包括它们的标题,并且您可能试图隐藏您的实现细节/解耦..
存储某种智能指针,例如unique_ptr
它实际上是一个对象,因此像对象一样被清理,如果存在则删除底层指针。
在您的类中存储一个常规指针,但在构造函数期间使用unique_ptr
(或者auto_ptr
如果unique_ptr
不可用),最后当您知道所有内容都已正确构造时,您可以将智能指针释放到您的成员变量中。
后一种解决方案如下所示:
// header file
//
class A; // implementation hidden on purpose
class B; // ditto
class Object
{
private:
A * a;
B * b;
public:
Object();
~Object();
private:
// if not using C++11 do not put in =delete but still declare these
Object( const Object & ) = delete;
Object& operator=( const Object & ) = delete;
};
// cpp file
#include "object.h"
#include "a.h"
#include "b.h"
Object::Object()
: a( nullptr ), // use NULL or 0 if nullptr not available
b( nullptr )
{
std::unique_ptr< A > pa( new A ); // might throw
std::unique_ptr< B > pb( new B ); // might throw (1)
a = pa.release(); // cannot throw
b = pb.release();
}
Object::~Object()
{
delete b;
delete a;
}
(1) 如果它抛出pa
which is local 将调用其析构函数,这将删除您使用 new 创建的指针。
注意:如果您没有unique_ptr
可用的,auto_ptr
也可以在这里提供。