gpt4 book ai didi

c++ - QJsonValueRef 与 QJsonValue

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:02 27 4
gpt4 key购买 nike

在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com