一個new的大概實現。所以delete 空指針是沒錯的,算是合法操作
// new circle(…)
{
void* temp = operator new(sizeof(circle));
try {
circle* ptr =
static_cast<circle*>(temp);
ptr->circle(…);
return ptr;
}
catch (...) {
operator delete(ptr);
throw;
}
}
//delete
if (ptr != nullptr) {
ptr->~shape();
operator delete(ptr);
}
//參考極客時間