C++ 新庫 - nothrow



描述

這是一個nothrow常量,此常量值用作operator new和operator new[]的引數,以指示這些函式在失敗時不應丟擲異常,而是返回空指標。

以下是std::nothrow的宣告。

		
extern const nothrow_t nothrow;

引數

返回值

異常

**不丟擲異常保證** - 此成員函式從不丟擲異常。

資料競爭

示例

以下是如何使用std::nothrow的示例。

#include <iostream>
#include <new>

int main () {
   std::cout << "Attempting to allocate...";
   char* p = new (std::nothrow) char [1024*1024];
   if (p==0) std::cout << "Failed!\n";
   else {
      std::cout << "Succeeded!\n";
      delete[] p;
   }
   return 0;
}

輸出應如下所示:

Attempting to allocate...Succeeded!
new.htm
廣告
© . All rights reserved.