如何在失败时返回 const QString 引用?

发布于 2024-08-23 02:50:44 字数 388 浏览 5 评论 0原文

考虑以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我很OK 2024-08-30 02:50:44

你不能。要么不返回 const 引用,要么使用局部静态变量,如下所示:

const QString& MyClass::getID(int index) const {
    if (i < myArraySize && (myArray[i] != 0)) {
        return myArray[i]->id; // id is a QString
    }

    static const QString emptyString;
    return emptyString;
}

与其他建议方法相比,此方法的优点是此解决方案不需要更改 MyClass 的接口。此外,使用默认参数可能会使类的用户感到困惑并导致错误的类使用。该解决方案对用户来说是透明的。

顺便问一下,你的类中真的使用了 C 风格的数组吗?

You can't. Either do not return a const reference or use a local static variable like this:

const QString& MyClass::getID(int index) const {
    if (i < myArraySize && (myArray[i] != 0)) {
        return myArray[i]->id; // id is a QString
    }

    static const QString emptyString;
    return emptyString;
}

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?

手长情犹 2024-08-30 02:50:44

由于这预计会返回一个 const 值,因此我认为拥有一个全局(或静态 const)空 QString 没有问题,所有此类函数都使用该 QString 返回一个空字符串。

不过我对这个名字并不感兴趣。我希望“空”QString 将是 QString 类的静态 const 成员。所以你的代码看起来像这样。

const QString& MyClass::getID(int index) const
{
    if (i < myArraySize && myArray[i]) {
        return myArray[i]->id; // id is a QString
    } else {
        return QString::EmptyString; // is a global empty QString
    }
}

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.

const QString& MyClass::getID(int index) const
{
    if (i < myArraySize && myArray[i]) {
        return myArray[i]->id; // id is a QString
    } else {
        return QString::EmptyString; // is a global empty QString
    }
}
鹿! 2024-08-30 02:50:44

如果不更改返回类型,就无法避免它。

如果您选择返回引用,那么您必须有一些返回类型的变量,该变量的寿命超出了函数的作用域。如果您无法更改 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.

巴黎盛开的樱花 2024-08-30 02:50:44

使用预先初始化的默认值怎么样:

const QString& MyClass::getID(int index, const QString& def = QString()) const
{
    if (i < myArraySize && myArray[index]) {
        return myArray[index]->id; // id is a QString
    } else {
        return def;
    }
}

How about using a pre-initialized default value:

const QString& MyClass::getID(int index, const QString& def = QString()) const
{
    if (i < myArraySize && myArray[index]) {
        return myArray[index]->id; // id is a QString
    } else {
        return def;
    }
}
一个人的旅程 2024-08-30 02:50:44

如果你坚持返回一个引用,你必须有一个对象可以引用;所以你的示例中必须有 QString 对象,没有办法解决它。

然而,一种似乎适合您情况的技术是更改您的方法以接受默认 ID 以在索引超出范围时返回:

const QString& MyClass::getID( int i, const QString& default ) const
{
  if( i < myArraySize && myArray[i] )
    return myArray[i]->id;
  else
    return default;
}

如果索引超出范围,您也可以抛出异常,那么您就不需要失败后真正返回,但这可能不是您想要的。

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:

const QString& MyClass::getID( int i, const QString& default ) const
{
  if( i < myArraySize && myArray[i] )
    return myArray[i]->id;
  else
    return default;
}

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.

两人的回忆 2024-08-30 02:50:44

QString::null 就足够了吗?

Would QString::null suffice?

请爱~陌生人 2024-08-30 02:50:44

如果不改变 getId() 的工作方式,就无法避免对空 QString 的需要。但我想到了两种方法:

  • 抛出异常,而不是默默地返回空字符串;或者
  • 不关心返回引用,只返回一个 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:

  • instead of silently returning an empty string, throw an exception; or
  • don't bother about returning a reference, and just return a QString, relying on return value optimization to eliminate the cost of copying the object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文