简体   繁体   中英

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. I don't see a problem with returning by value, that is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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