theScore said:
nullptr or NULL with old C++ but I don't understand why ?
It is simple a matter of safety. NULL is defined as 0 (zero) so assigning 0 to a pointer sets it's address to zero and the pointer is invalid in case of
if(ptr)
{...}
which you should use instead of your equality check because the compiler will throw errors for some types that don't be equotable.
However, what happens if you try to pass NULL to a function that has overloads for a pointer and an arithmetic value, how do you deduce which overload to use? The answer is that it will simply set the value to 0 rather than passing the pointer as a null pointer, so this is considered to be bad.
I'm still using old C++ code and made my own null_ptr implementation that is simply defined as
/**
Null-Pointer Type
*/
const struct
{
public:
template<class T> force_inline operator T*() const
{
return 0;
}
template<class C, class T> force_inline operator T C::*() const
{
return 0;
}
private:
void force_inline operator&() const {}
} NullPointer = { };
As you can see, the code has two operators that both return certain kind of pointer which is deduced at compile time. Now you can pass NullPointer to a function and will always invoke the pointer overload