PDA

View Full Version : Parameter passing and return values of type QString/QString&



hackerNovitiate
30th January 2010, 13:24
Hi all,

I'm having some difficulty understanding how to properly (and efficiently) write functions that receive/return QStrings. My first programming language is C and I'm very comfortable with pointers, but I only had a short crash course in C++ so I still need to pause and think whenever using references.

Anyway, Trolltech's QString Class Reference page says that "QStrings may be treated like ints or other basic types". But if that's the case, why do some QString methods return QString (e.g. QString::simplified() const ), while others return QString& (e.g. QString::remove(int, int) )?

Also, why must parameters be passed as "const QString &", rather than just "const QString" (e.g. QString(const QString & other) )?


Thanks in advance!

wysota
30th January 2010, 13:27
Anyway, Trolltech's QString Class Reference page says that "QStrings may be treated like ints or other basic types". But if that's the case, why do some QString methods return QString (e.g. QString::simplified() const ), while others return QString& (e.g. QString::remove(int, int) )?
Because remove() works directly on the string and doesn't return a copy but rather the string itself (thus the reference and not a value is returned).


Also, why must parameters be passed as "const QString &", rather than just "const QString" (e.g. QString(const QString & other) )?
const QString would be fine too but it would require the string to be copied and the const modifier wouldn't have any practical meaning. With passing a const reference no copy is made.

hackerNovitiate
31st January 2010, 14:15
Because remove() works directly on the string and doesn't return a copy but rather the string itself (thus the reference and not a value is returned).
Hmm... to clarify, can you please check if the following are correct?
1) remove() modifies the original string, and returns the same string
2) simplified() leaves the original string untouched, and returns a modified copy
3) A function that returns "QString" will return a shallow copy of the string, so this CAN used to return a local variable
4) A function that returns "QString&" will return a reference to an existing string, so this MUST be used to return a local variable

franz
31st January 2010, 14:58
Hmm... to clarify, can you please check if the following are correct?
1) remove() modifies the original string, and returns the same string
Yes.



2) simplified() leaves the original string untouched, and returns a modified copy

Correct


3) A function that returns "QString" will return a shallow copy of the string, so this CAN used to return a local variable

Correct


4) A function that returns "QString&" will return a reference to an existing string, so this MUST be used to return a local variable
Actually, you might run into trouble when referencing a local variable that is returned from a string. The variable will be destructed when the function ends, leaving the reference referencing a variable that doesn't exist anymore (which shouldn't happen). You return a reference when you want the calling function to be able to edit the value in place (in a QList for example).


QStringList strings;
strings << "string1" << "string2";
strings[0].replace('1','0');

This edits the string at location 0 to change every 1 into a 0.
QString::remove() returns a reference to itself, so you can do the following:


QString txt = "some text";
txt.remove(8, 1).remove(5, 2);
// txt is now "some x"

hackerNovitiate
1st February 2010, 14:21
Whoops, I meant to say "must not" in my last post. wysota, thanks for the explanation; franz, thanks for the removing my lingering uncertainties and for the insightful example using references with lists.