new/delete

一個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);
}

//參考極客時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章