How to return a const QString reference in case of failure?

Posted by moala on Stack Overflow See other posts from Stack Overflow or by moala
Published on 2010-02-28T21:11:07Z Indexed on 2010/04/30 4:27 UTC
Read the original article Hit count: 171

Filed under:
|
|

Hi, 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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt