nullptr
It is new keyword in c++11. It has type std::nullptr_t.
We should prefer nullptr instead of NULL and ‘0’ because ‘0’ is int type and NULL is long type. These both don’t have pointer type. There is one situation(“overloaded function with integer and pointer”) where type of NULL or ‘0’ could lead to wrong direction. For detail information about “overloaded function with integer and pointer” please check below code.
Complete source code with example:
#include<iostream> void fun(int i) { std::cout<<"In fun integer as argument"<<std::endl; return; } void fun(void* ptr) { std::cout<<"In fun pointer as argument"<<std::endl; } int main() { std::cout<<"nullptr c++11"<<std::endl; fun(0); //fun(NULL);//we will get below error //error: call of overloaded ‘fun(NULL)’ is ambiguous fun(nullptr); return 0; }
Output:
nullptr c++11 In fun integer as argument In fun pointer as argument