Concatenating strings: instantiating QString?
-
wrote on 25 May 2012, 15:26 last edited by
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.
-
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 :)
-
wrote on 5 Jun 2012, 09:29 last edited by
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.
-
wrote on 5 Jun 2012, 11:11 last edited by
@
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.hifndef 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; }
@ -
wrote on 5 Jun 2012, 11:19 last edited by
Whoot? That is evil :-)
Nevertheless, I stand corrected. -
wrote on 5 Jun 2012, 11:21 last edited by
Not every evil is bad :-)
Some leftovers from Qt 3 times, I would guess. -
wrote on 6 Jun 2012, 20:46 last edited by
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...) -
wrote on 7 Jun 2012, 08:39 last edited by
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.