Search code examples
c++qtshared-ptrqsharedpointer

Difference between QSharedPointer::isNull() and operator!()


in Qt docs we read:

bool QSharedPointer::operator! () const

Returns true if this object is null. 
This function is suitable for use in if-constructs, like:

 if (!sharedptr) { ... }

and

bool QSharedPointer::isNull () const
Returns true if this object is holding a reference to a null pointer.

What is the difference between these two functions? This is clear what is reference to a null pointer, but what means here

"if the object is null" ?

What determines if QSharedPointer is null? How do these functions correspond to QSharedPointer::data() != null ?


Solution

  • From Qt sources of the QSharedPointer class:

    inline bool operator !() const { return isNull(); }
    

    This confirms what @JoachimPileborg said in his comment - isNull() function and operator!() are equivalent.