假设我有一个充当“智能指针”的类,并在销毁时释放某种系统资源。
class Resource{
protected:
ResourceHandle h;
public:
Resource(ResourceHandle handle)
:h(handle){
}
~Resource(){
if (h)
releaseResourceHandle(h);//external function, probably from OS
}
};
我有一些函数返回用于初始化“资源”的值:
ResourceHandle allocateHandle();
现在,如果我在我的代码中这样做:
Resource resource(allocateHandle());
AND allocateHandle()抛出异常,究竟会发生什么?崩溃会发生在 Resource() 构造期间还是构造之前?
常识告诉我,因为在 allocateHandle 返回之前抛出异常,执行甚至不会进入 Resource() 构造函数,但我不确定。这是一个正确的假设吗?