Separate nullptr in C++11

In my earlier articles on C++, I have written about the automatic type detection, uniform initialization list and Range based for loopin C++11.

Earlier we used to use the NULL macro which is synonymous to int zero. This came as an inheritance from C language where 0 plays a double role (as an int and as NULL pointer).

NULL pointer in C language is defined as either ((void *) 0 ) or 0 (int), but in C++ it is defined as int 0 for most of the cases. If NULL is defined as 0 (int zero), and there are two overloaded functions:

   void fun(int);
   void fun(int*);
Now function call
    fun( NULL );
will call fun(int)which may not be the intention. To overcome this ambiguity C++ introduced new, strongly-typed keyword nullptrnullptr is applicable to all pointer categories including function pointers and pointer to class members.
    fun(0);        // Call fun(int)
    fun(nullptr);  // call fun( int* )

Tags:

CPP NULLptr

0 Comments

Leave a comment