Question

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 ?

Was it helpful?

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.

OTHER TIPS

A "null" QSharedPointer wraps a T* t where t equals 0/NULL/nullptr. That's what's meant with "object is null"

isNull() and operator!() are equivalent, you can use either one.

A shared pointer is null by default, or when set to 0/nullptr explicitly:

QSharedPointer<T> t; //null
QSharedPointer<T> t2(new T); //not null
QSharedPointer<T> t3(0); //null
QSharedPointer<T> t4(nullptr); //null
t2.clear(); //not null before, now null
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top