简体   繁体   English

C ++ Qt返回对临时引用

[英]C++ Qt Returning reference to a temporary

I cannot understand how to return a hashed string from function temporary memory (don't know how to correctly call it). 我不明白如何从函数临时存储器中返回哈希字符串(不知道如何正确调用它)。 Now, I have this code: 现在,我有以下代码:

static const QString &Utils::md5(const QString &inStr)
{
    const QByteArray out = QCryptographicHash
            ::hash(inStr.toUtf8(), QCryptographicHash::Md5)
            .toHex();
    return QString(out);
}

But it gives warning during compilation and after I run my program it crashes. 但是它在编译期间发出警告,并且在我运行程序后崩溃。

Yes, you can't return a reference to a local object, even reference-to-const. 是的,您不能返回对本地对象的引用,甚至不能返回对const的引用。 I don't see a problem with returning by value, that is: 我看不到按值返回的问题,即:

static QString Utils::md5(const QString &inStr) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM