共用方式為


nullptr (C++/CLI 和 C++/CX)

關鍵字 nullptr 代表 Null 指標值 。 使用 null 指標值來指出物件控制代碼、內部指標或原生指標型別並未指向物件。

搭配 Managed 或機器碼使用 nullptr 。 編譯器會針對受控和原生的 null 指標值發出適當但不同的指示。 如需使用此關鍵字的 ISO 標準 C++ 版本相關資訊,請參閱 nullptr

__nullptr 關鍵字是 Microsoft 特定的關鍵字,其意義 nullptr 與 相同,但僅適用于機器碼。 如果您使用 nullptr 原生 C/C++ 程式碼,然後使用 /clr 編譯器選項進行編譯 ,編譯器無法判斷是否 nullptr 表示原生或 Managed Null 指標值。 若要讓編譯器清楚您的意圖,請使用 nullptr 來指定 Managed 值或 __nullptr 來指定原生值。

關鍵字 nullptr 相當於 Visual Basic 中的 Nothing ,在 C# 中為 Null

使用方式

關鍵字 nullptr 可以在控制碼、原生指標或函式引數的任何位置使用。

nullptr關鍵字不是類型,不支援搭配使用:

  • sizeof

  • typeid

  • throw nullptr (儘管 throw (Object^)nullptr; 將能運作)

nullptr關鍵字可用於下列指標類型的初始化:

  • 原生指標

  • Windows 執行階段控制代碼

  • 受控的控制代碼

  • 受控的內部指標

nullptr關鍵字可用來測試指標或控制碼參考在使用參考之前是否為 Null。

在語言之間使用 null 指標值來檢查錯誤的函式呼叫應會正確解譯。

您無法將控制碼初始化為零;只能 nullptr 使用。 為物件控制代碼指派常數 0 會產生 Boxed 的 Int32Object^ 的轉換。

範例: nullptr 關鍵字

下列程式碼範例示範 nullptr ,只要可以使用控制碼、原生指標或函式引數,就可以使用 關鍵字。 而此範例示範 nullptr 關鍵字可用來檢查參考,然後再使用它。

// mcpp_nullptr.cpp
// compile with: /clr
value class V {};
ref class G {};
void f(System::Object ^) {}

int main() {
// Native pointer.
   int *pN = nullptr;
// Managed handle.
   G ^pG = nullptr;
   V ^pV1 = nullptr;
// Managed interior pointer.
   interior_ptr<V> pV2 = nullptr;
// Reference checking before using a pointer.
   if (pN == nullptr) {}
   if (pG == nullptr) {}
   if (pV1 == nullptr) {}
   if (pV2 == nullptr) {}
// nullptr can be used as a function argument.
   f(nullptr);   // calls f(System::Object ^)
}

範例:以交換的方式使用 nullptr 和零

下列程式碼範例顯示 和 nullptr 零可以交替使用於原生指標。

// mcpp_nullptr_1.cpp
// compile with: /clr
class MyClass {
public:
   int i;
};

int main() {
   MyClass * pMyClass = nullptr;
   if ( pMyClass == nullptr)
      System::Console::WriteLine("pMyClass == nullptr");

   if ( pMyClass == 0)
      System::Console::WriteLine("pMyClass == 0");

   pMyClass = 0;
   if ( pMyClass == nullptr)
      System::Console::WriteLine("pMyClass == nullptr");

   if ( pMyClass == 0)
      System::Console::WriteLine("pMyClass == 0");
}
pMyClass == nullptr

pMyClass == 0

pMyClass == nullptr

pMyClass == 0

範例:解譯 nullptr 為控制碼

下列程式碼範例顯示,解 nullptr 譯為任何型別的控制碼或任何型別的原生指標。 如果使用不同型別的控制代碼進行函式多載,將產生模稜兩可錯誤。 nullptr必須明確地轉換成型別。

// mcpp_nullptr_2.cpp
// compile with: /clr /LD
void f(int *){}
void f(int ^){}

void f_null() {
   f(nullptr);   // C2668
   // try one of the following lines instead
   f((int *) nullptr);
   f((int ^) nullptr);
}

範例:轉換 nullptr

下列程式碼範例顯示允許轉換 nullptr ,並傳回包含 nullptr 值的轉換類型指標或控制碼。

// mcpp_nullptr_3.cpp
// compile with: /clr /LD
using namespace System;
template <typename T>
void f(T) {}   // C2036 cannot deduce template type because nullptr can be any type

int main() {
   f((Object ^) nullptr);   // T = Object^, call f(Object ^)

   // Delete the following line to resolve.
   f(nullptr);

   f(0);   // T = int, call f(int)
}

範例:以函式參數的形式傳遞 nullptr

下列程式碼範例示範 nullptr 可用來做為函式參數。

// mcpp_nullptr_4.cpp
// compile with: /clr
using namespace System;
void f(Object ^ x) {
   Console::WriteLine("test");
}

int main() {
   f(nullptr);
}
test

範例:預設初始化

下列程式碼範例示範當控制碼宣告且未明確初始化時,預設會初始化為 nullptr

// mcpp_nullptr_5.cpp
// compile with: /clr
using namespace System;
ref class MyClass {
public:
   void Test() {
      MyClass ^pMyClass;   // gc type
      if (pMyClass == nullptr)
         Console::WriteLine("NULL");
   }
};

int main() {
   MyClass ^ x = gcnew MyClass();
   x -> Test();
}
NULL

範例:指派 nullptr 給原生指標

下列程式碼範例示範 nullptr 當您使用 /clr 編譯時,可以指派給原生指標。

// mcpp_nullptr_6.cpp
// compile with: /clr
int main() {
   int * i = 0;
   int * j = nullptr;
}

需求

編譯器選項:(不需要;所有程式碼產生選項都支援,包括 /ZW/clr

另請參閱

適用於.NET 和 UWP 的元件延伸模組
nullptr