Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]how to return an empty QString

[SOLVED]how to return an empty QString

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 6.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    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)
    }
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3

        thank you very much:)

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

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

          Thanks.

          Be Cute

          1 Reply Last reply
          0
          • O Offline
            O Offline
            opengpu
            wrote on last edited by
            #5

            ok
            got it, thank you

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andrep
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved