C++ 11
nullptr
nullptr 是用于解决 NULL 和 0 的有疑义关系的。NULL 通常被义为(void*)0。在
如下应用中会引发歧义。
#include <iostream>
using namespace std;
void f(int){}
void f(bool){}
void f(void*){}
int main()
{
f(0);// calls f(int), not f(void*)
f(NULL);// might not compile, but typically calls f(int). Never calls f(void*)
f(nullptr); // calls f(void*) overload
}
1) C++ 视 0 首先为 int 型,因此,调用 f(0) 即调用 f(int)
2) NULL 的情况复杂些,C++ 首先视其为广义整型。假如 NULL 被定义为普通
的 0,则调用 f(int);
如果 NULL 被定义成 0L,则 long -> int, long -> bool, 0L -> void*, 这三
种情况都是合法的,此时,编译器会报错
3) 使用 nullptr,则不会有重载函数调用模糊的问题
- nullptr 不属于广义整型,也不是普通意义上的指针。
- nullptr 的实际类型是 std::nullptr_t,它能够隐式的转换成所有的原始指针
类型,故可将其视为一个可指向所有类型的指针。
final
关键字 final 有两个用途。第一,它阻止了