日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++11中nullptr的使用

發(fā)布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11中nullptr的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在C語言中,NULL實際上是一個void* 的指針,然后把void* 指針賦值給其它類型的指針的時候,會隱式轉(zhuǎn)換成相應的類型。而如果用一個C++編譯器來編譯的時候是要出錯的,因為C++是強類型的,void* 是不能隱式轉(zhuǎn)換成其它指針類型的。在C++中為了解決空指針的問題,在C++中引入0來表示空指針。NULL無類型,它是一個宏。nullptr是有類型的,類型是std::nullptr_t。

推薦:純C語言用NULL;C++用0;如果編譯器支持nullptr,使用nullptr。

The keyword nullptr denotes the pointer literal. It is a prvalue(pure rvalue) of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL.

Both true and false are keywords and as literals they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, and it's a prvalue (you cannot take the address of it using &).

A null pointer constant with the nullptr value has the following characteristics:

(1)、It can be converted to any pointer or pointer-to-member type.

(2)、It cannot be implicitly converted to any other type, except for the bool type.

(3)、It cannot be used in an arithmetic expression.

(4)、It can be compared with the integer 0.

(5)、It can be used in relational expressions to compare with pointers or data of the std::nullptr_t type.

It should be noted that in C++11 it is still acceptable to assign the value 0 or NULL to a pointer.

在VS2013中,NULL的定義是在stdio.h文件中,如下:

/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else  /* __cplusplus */
#define NULL    ((void *)0)
#endif  /* __cplusplus */
#endif  /* NULL */

下面是從其他文章中copy的測試代碼,詳細內(nèi)容介紹可以參考對應的reference:

#include "nullptr.hpp"#include <iostream>
#include <cstddef> // for std::nullptr_t///
// reference: http://en.cppreference.com/w/cpp/language/nullptr
template<class F, class A>
void Fwd(F f, A a)
{f(a);
}void g(int* i)
{std::cout << "Function g called\n";
}int test_nullptr1()
{g(NULL);           // Fineg(0);              // FineFwd(g, nullptr);   // Fine// Fwd(g, NULL);  // ERROR: No function g(int) // error C2664: “void (int *)”: 無法將參數(shù) 1 從“int”轉(zhuǎn)換為“int *”int length1 = sizeof(NULL); // x64, length1 = 4int length2 = sizeof(nullptr); // x64, length2 = 8return 0;
}///
// reference: https://msdn.microsoft.com/zh-cn/library/4ex65770.aspx
class MyClass {
public:int i;
};int test_nullptr2()
{MyClass * pMyClass = nullptr;if (pMyClass == nullptr)std::cout << "pMyClass == nullptr" << std::endl; // pMyClass == nullptrif (pMyClass == 0)std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0pMyClass = 0;if (pMyClass == nullptr)std::cout << "pMyClass == nullptr" << std::endl; // pMyClass == nullptrif (pMyClass == 0)std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0return 0;
}/
void f(int *)
{std::cout << "f(int *)" << std::endl;;
}void f(int &)
{std::cout << "f(int &)" << std::endl;
}int test_nullptr3()
{f(nullptr); // f(int *)// try one of the following lines insteadf((int *) nullptr); // f(int *)f(0); // f(int *)f(NULL); // f(int *)//f((int &) nullptr); // error C2101: 常量上的“&”return 0;
}//
// reference: http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html
void func(int n)
{std::cout << "func (int n)" << std::endl;
}void func(char *s)
{std::cout << "func (char *s)" << std::endl;
}int test_nullptr4()
{func(0); // func (int n)func(NULL); // func (int n)func(nullptr); // func (char *s)return 0;
}//
// reference: http://www.learncpp.com/cpp-tutorial/6-7a-null-pointers/
void doSomething(int *ptr)
{if (ptr)std::cout << "You passed in " << *ptr << '\n';elsestd::cout << "You passed in a null pointer\n";
}void doSomething_(std::nullptr_t ptr)
{std::cout << "in doSomething_()\n";
}int test_nullptr5()
{int* a = NULL; // ok//int* b = (void*)0; // error C2440: “初始化”: 無法從“void *”轉(zhuǎn)換為“int *”int* c = 0; // ok// the argument is definitely a null pointer (not an integer)doSomething(nullptr); // You passed in a null pointerdoSomething(0); // You passed in a null pointerdoSomething(NULL); // You passed in a null pointer// call doSomething_ with an argument of type std::nullptr_tdoSomething_(nullptr); // in doSomething_()return 0;
}

GitHub:https://github.com/fengbingchun/Messy_Test

總結(jié)

以上是生活随笔為你收集整理的C++11中nullptr的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉(zhuǎn)載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:C++11中nullptr的使用