c++ nullptr vs NULL

看到c++ prime 5中page 48 中介绍了初始化指针的介绍

int *p1 = nullptr;
int *p2 = 0;
int *p3 = NULL;
项目开发中一般都是把指针赋值NULL,因为0是给int类型初始化的,那么nullpter是什么鬼,

stack flow的解答

nullptr is always a pointer type. 0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:

If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

查阅资料得知,nullptr是在c++ 11 中出现的,在c++ 11之前没有明确的给指针对象初始化的初始值,所以用NULL 过渡下其实NULL 在系统的宏定义是定义成0的,在c++11之后出现了给指针对象初始化的初始值nullptr


你可能感兴趣的:(c++)