这里是这个函数的文档,
T & QList::first ()
Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call
isEmpty()
before calling this function.
我想知道如果列表为空,QList::first()
会返回什么?
最佳答案
源代码的快速扫描(引用:qtproject/qt
)显示调用受 QT_ASSERT(!isEmpty())
保护(对于两个覆盖,const
和非)。
如果不深入研究源代码,我猜测 QT_ASSERT
将调用 terminate
或类似的方法(如果在调试中构建),并且可能会在发布时编译出来。否则,对 QList::first
的调用最终会取消引用代理类型 QListData::Data
中的指针,这在您的情况下是未定义的行为。
总结:不要这样做
关于c++ - 如果列表为空,QList::first() 会返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33239905/