class test{
int value;
};
class sample{
sample(){
test *var = new test();
}
};
int main(){
sample foo;
//what will happen here if sample constructor fails to allocate its memory?
}
在C++構造函數中使用new
運算符是否正確? 如果sample
構造函數未能分配其內存,會發生什麼?在C++構造函數中使用new運算符是否正確?
將引發異常。 – StoryTeller
...或返回nullptr –
@EdHeal,這不是'std :: nothrow'重載調用,並且'operator new'不會超載。所以一個例外,絕對是一個例外。 – StoryTeller