如何在失败时返回 const QString 引用?
考虑以下代码:
const QString& MyClass::getID(int index) const
{
if (i < myArraySize && myArray[i]) {
return myArray[i]->id; // id is a QString
} else {
return my_global_empty_qstring; // is a global empty QString
}
}
如何在不更改方法的返回类型的情况下避免出现空的 QString? (似乎返回在堆栈上分配的空 QString 是一个坏主意)
谢谢。
consider the following code:
const QString& MyClass::getID(int index) const
{
if (i < myArraySize && myArray[i]) {
return myArray[i]->id; // id is a QString
} else {
return my_global_empty_qstring; // is a global empty QString
}
}
How can I avoid to have an empty QString without changing the return type of the method? (It seems that returning an empty QString allocated on the stack is a bad idea)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你不能。要么不返回 const 引用,要么使用局部静态变量,如下所示:
与其他建议方法相比,此方法的优点是此解决方案不需要更改
MyClass
的接口。此外,使用默认参数可能会使类的用户感到困惑并导致错误的类使用。该解决方案对用户来说是透明的。顺便问一下,你的类中真的使用了 C 风格的数组吗?
You can't. Either do not return a const reference or use a local static variable like this:
The advantage of this method over the other proposed methods is that this solution does not require a change to the interface of
MyClass
. Furthermore, using a default parameter might confuse users of your class and lead to wrong class usage. This solution is transparent to the user.By the way, are you really using a C style array in your class?
由于这预计会返回一个 const 值,因此我认为拥有一个全局(或静态 const)空 QString 没有问题,所有此类函数都使用该 QString 返回一个空字符串。
不过我对这个名字并不感兴趣。我希望“空”QString 将是 QString 类的静态 const 成员。所以你的代码看起来像这样。
Since this is expected to return a
const
value I see no problem with having a global (or static const) empty QString that is used by all such functions to return a an empty string.I'm not wild about the name though. I would expect that the "empty" QString would be a static const member of the QString Class. so your code would look like this instead.
如果不更改返回类型,就无法避免它。
如果您选择返回引用,那么您必须有一些返回类型的变量,该变量的寿命超出了函数的作用域。如果您无法更改 API(例如,由于二进制兼容性承诺),那么您将永远被锁定。即使您更改类实现的其余部分以例如动态生成值或从某些外部源检索它们,您也必须浪费内存来存储相关类型的某些值。
这就是为什么意识到二进制兼容性问题的 C++ API 设计指南建议在没有仔细考虑的情况下不要返回
const&
。You can't avoid it without changing the return type.
If you choose to return a reference, then you must have some variable of the return type which outlives the function's scope. If you can't change the API (e.g. due to binary compatibility promises), then you are locked in to this forever. You'll have to waste memory storing some value of the relevant type, even if you change the rest of your class implementation to e.g. generate the values on the fly or retrieve them from some external source.
This is why C++ API design guides which are aware of binary compatibility issues recommend to not return a
const&
without careful consideration.使用预先初始化的默认值怎么样:
How about using a pre-initialized default value:
如果你坚持返回一个引用,你必须有一个对象可以引用;所以你的示例中必须有 QString 对象,没有办法解决它。
然而,一种似乎适合您情况的技术是更改您的方法以接受默认 ID 以在索引超出范围时返回:
如果索引超出范围,您也可以抛出异常,那么您就不需要失败后真正返回,但这可能不是您想要的。
if you insist on returning a reference, you must have an object to refer to; so you must have the QString object somewhere in your example, there is no way around it.
However a technique that seems suitable for your case is to change your method to accept a default ID to return in case the index is out of range:
You could also throw an exception if the index is out of range, then you wouldn't need to actually return on failure but that's probably not what you want.
QString::null 就足够了吗?
Would QString::null suffice?
如果不改变
getId()
的工作方式,就无法避免对空 QString 的需要。但我想到了两种方法:You cannot avoid the need for an empty QString without changing the way
getId()
works. But there are two approaches that spring to mind: