QSharedPointer :: isNull()和operator!()之间的区别

4pi*_*ie0 3 c++ qt shared-ptr qsharedpointer

在Qt文档中我们读到:

bool QSharedPointer::operator! () const

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

 if (!sharedptr) { ... }
Run Code Online (Sandbox Code Playgroud)

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

这两个功能有什么区别?这很清楚什么是对空指针的引用,但这意味着什么

"如果对象为空"?

什么决定是否QSharedPointer 为空?这些功能如何对应QSharedPointer::data() != null

vah*_*cho 5

从Qt QSharedPointer类来源:

inline bool operator !() const { return isNull(); }
Run Code Online (Sandbox Code Playgroud)

这证实了@JoachimPileborg在他的评论中所说的 - isNull()功能和operator!()等价物.