Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Concatenating strings: instantiating QString?

Concatenating strings: instantiating QString?

Scheduled Pinned Locked Moved C++ Gurus
8 Posts 5 Posters 9.3k 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.
  • D Offline
    D Offline
    disperso
    wrote on 25 May 2012, 15:26 last edited by
    #1

    Hi.

    It's been a while since I don't post here because it's been a while without Qt/C++ coding, so I'm very rusty. I'm so rusty that the title is probably very bad (feel free to suggest a better one). What I mean is the following:

    In the Qt documentation there is an example that says something like this:
    @
    QString locale = QLocale::system().name();
    translator.load(QString("filename_") + locale);
    @

    My question is: why wrapping "filename_" in a QString? Without it, the variable that is already of type QString can be concatenated with the literal char*. Isn't this instantiation of QString unnecessary? Or it happens implicitly anyway? And if it happens implicitly anyway, isn't that longer to write for no reason?

    I know it sounds picky, but I want to understand well this kind of things, because I think it can cause real errors in other cases if I don't know 100% of what I'm doing. :)

    Thank you guys.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 26 May 2012, 09:36 last edited by
      #2

      Not sure, it might have something to do with the addition of QStringBuilder and QStringLiteral. QSB used to throw errors for char* and QString concatenation. Or maybe just some random QString addition. Maintaining so huge documentation is not easy :)

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miroslav
        wrote on 5 Jun 2012, 09:29 last edited by
        #3

        You could try

        @
        translator.load("filename_" + locale);
        @

        Now the first string is a simple character array. This would not work, because the + operator for it is not defined to concatenate strings.

        Mirko Boehm | mirko@kde.org | KDE e.V.
        FSFE Fellow
        Qt Certified Specialist

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 5 Jun 2012, 11:11 last edited by
          #4

          @
          QString("filename_") + locale
          @

          Does always work, as there is no implicit conversion applied.

          @
          "filename_" + locale
          @

          Does work if you do not have defined QT_NO_CAST_FROM_ASCII. I.e. it can break in a different build environment.

          @miroslav
          Qt defines an operator with char array left hand and QString righthand:

          @
          // in qstring.h

          ifndef QT_NO_CAST_FROM_ASCII

          // ...
          inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
          { QString t = QString::fromAscii(s1); t += s2; return t; }
          @

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            miroslav
            wrote on 5 Jun 2012, 11:19 last edited by
            #5

            Whoot? That is evil :-)
            Nevertheless, I stand corrected.

            Mirko Boehm | mirko@kde.org | KDE e.V.
            FSFE Fellow
            Qt Certified Specialist

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on 5 Jun 2012, 11:21 last edited by
              #6

              Not every evil is bad :-)
              Some leftovers from Qt 3 times, I would guess.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DerManu
                wrote on 6 Jun 2012, 20:46 last edited by
                #7

                Volker/Miro, are you saying one shouldn't use this easier way of concatenating strings anymore in Qt4 and 5 ("evil", "leftover")?

                I use it all the time, it feels like a very natural way of handling strings. :/
                (Yeah I know the tr("...%1%2..").arg(..).arg(..) technique is the way to go for many gui strings, but sometimes it's just easier and sufficient to just concatenate with the overloaded char* + operator...)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  miroslav
                  wrote on 7 Jun 2012, 08:39 last edited by
                  #8

                  I am not saying concatenating strings using + is wrong. Far from it. But it should only be applied to class types.

                  What I am saying is that overloading the + operator with a left hand argument of a primitive type is evil. A const char* is effectively the address of some memory, and the semantics of operator+() for such types are defined, if archaic.

                  Mirko Boehm | mirko@kde.org | KDE e.V.
                  FSFE Fellow
                  Qt Certified Specialist

                  1 Reply Last reply
                  0

                  • Login

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