[SOLVED]how to return an empty QString



  • i want to make the API safe
    so i write like this:
    @
    const QString Class::GetName() const
    {
    if(m_pEdit) return m_pEdit->text();
    else return NULL; //what is the normally way to do this. //does return QString() is better.(does this return an Empty string)
    }
    @


  • Moderators

    Hi. PLease use '@' tags to enclose source code on this forum - it makes it much easier to read. I've already done that in your post.

    To return an empty string, you can do those:
    @
    return QString(); // 1
    return ""; // 2
    return QString::null; // 3

    QString result;
    return result; // 4
    @

    All those are valid and fine. I would personally go with 4 or 3. The benefit in 3 (although I'm not sure it will compile in this specific case) is that QString::null does not create a QString object, so it saves a tiny bit of CPU power and memory.



  • thank you very much:)



  • Hi.
    it seems that your problem is solved so update your thread title as (SOLVED)

    Thanks.



  • ok
    got it, thank you



  • 1 and 3 are practically the same (single assignment of the internal data pointer). Given that 1. is the least amout to type I'd call it "preferable". Depending on circumstances/optimizer/... 4 could produce the same code, but I wouldn't rely on that. 2 runs through several out-of-line functions and is uniformly worse, it will not even compile if e.g. QT_NO_CAST_FROM_ASCII is used.

    Note that returning QString::null does not prevent the creation of a real QString object.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.