nullptr (C++ 元件擴充功能)
nullptr 關鍵字表示 null 指標值。使用 null 指標指出物件控制代碼、內部指標或原生指標型別不會指向物件。
使用與 Managed 或機器碼的 nullptr 。編譯器發出 Managed 和原生 null 指標值的適當,但不同的指示。如需使用這個關鍵字 ISO 標準 C++ 版本的詳細資訊,請參閱 nullptr。
__nullptr 關鍵字是具有意義和 nullptr的 Microsoft 特定關鍵字,不過,僅適用於機器碼。如果您使用與原生 C/C++ 程式碼的 nullptr 然後編譯 /clr 編譯器選項,編譯器無法判斷 nullptr 指出原生或已處理的 null 指標值。若要指出您要編譯器,請使用 nullptr 指定 Managed 值或 __nullptr 指定原生值。
nullptr 關鍵字與在 Visual Basic 中 Nothing 和 null 相當於 C# 中的。
使用方式
可以使用 nullptr 關鍵字任何地方控制代碼,原生指標,或者可以使用函式引數。
nullptr 關鍵字不與不支援以搭配使用:
nullptr 關鍵字可在下列指標型別的初始化:
原生指標。
Windows 執行階段控制代碼
Managed 控制代碼
已處理的內部指標。
nullptr 關鍵字可用來測試,如果指標或控制代碼參考是 null,才能使用參考。
在使用的語言中的函式呼叫 null 指標為錯誤檢查值應該正確地解譯。
您無法使用控制代碼為零;只能使用 nullptr 。常數值指派給物件控制代碼的產生 Boxed Int32 和一個轉換成 Object^。
範例
下列程式碼範例示範,可以使用 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 和零在原生指標可以交替使用。
// 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");
}
Output
範例
下列程式碼範例中,示範了 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 包含值的轉換型別。
// 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 可做為函式參數。
// mcpp_nullptr_4.cpp
// compile with: /clr
using namespace System;
void f(Object ^ x) {
Console::WriteLine("test");
}
int main() {
f(nullptr);
}
Output
範例
下列程式碼範例中,示範了時,控制代碼宣告和明確初始化時,它們是預設初始化為 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();
}
Output
範例
下列程式碼範例中,示範了 nullptr 可指派給原生指標,以編譯 /clr時。
// mcpp_nullptr_6.cpp
// compile with: /clr
int main() {
int * i = 0;
int * j = nullptr;
}
需求
編譯器選項:(不需要;支援的任何程式碼產生選項,包括 /ZW 和 /clr)