- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Qt 的 JSON 实现中,在 QJsonObject
类中,有两个运算符的实现(文档 here ):
QJsonValue QJsonObject::operator[](const QString & key) const;
QJsonValueRef QJsonObject::operator[](const QString & key);
首先,返回 QJsonValueRef
与返回 QJsonValue
相比有什么优势?其次,如果我只是说类似 root['time']
的内容,将返回哪个值,其中 root
是一个 QJsonObject
?
最佳答案
您应该避免在提交的问题中提出多个问题。话虽如此,以下是您问题的答案:
Returns a reference to the value for key.
The return value is of type QJsonValueRef, a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference.
这意味着,您可以在返回值上调用方法,而无需在代码中显式创建临时对象,就像引用在 C++ 中的工作方式一样。
至于第二个子问题,要看根对象是什么。如果它是一个 const 对象,则不能调用第二个非 const 版本,因为这会违反 const 的正确性。注意最后的常量:
> QJsonValue QJsonObject::operator[](const QString & key) const;
^^^^^
对于可变的,又名。非常量对象,您可以调用两者,但默认情况下会调用第二个版本。然而,通过一些 const 转换,这可以改变。
关于c++ - QJsonValueRef 与 QJsonValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708903/
在 Qt 的 JSON 实现中,在 QJsonObject 类中,有两个运算符的实现(文档 here ): QJsonValue QJsonObject::operator[](const QStri
我在 Qt 中使用 JSON 已经有一段时间了,而且我总是按照 example 中显示的方式使用它。 .但是,我想知道在将项目添加到 QJsonObject 时,直接分配给 QJsonValueRef
我是一名优秀的程序员,十分优秀!