Results 1 to 5 of 5

Thread: Parameter passing and return values of type QString/QString&

  1. #1
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Parameter passing and return values of type QString/QString&

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Parameter passing and return values of type QString/QString&

    Quote Originally Posted by hackerNovitiate View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    hackerNovitiate (1st February 2010)

  4. #3
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parameter passing and return values of type QString/QString&

    Quote Originally Posted by wysota View Post
    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

  5. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parameter passing and return values of type QString/QString&

    Quote Originally Posted by hackerNovitiate View Post
    Hmm... to clarify, can you please check if the following are correct?
    1) remove() modifies the original string, and returns the same string
    Yes.

    Quote Originally Posted by hackerNovitiate View Post
    2) simplified() leaves the original string untouched, and returns a modified copy
    Correct
    Quote Originally Posted by hackerNovitiate View Post
    3) A function that returns "QString" will return a shallow copy of the string, so this CAN used to return a local variable
    Correct
    Quote Originally Posted by hackerNovitiate View Post
    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).
    Qt Code:
    1. QStringList strings;
    2. strings << "string1" << "string2";
    3. strings[0].replace('1','0');
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QString txt = "some text";
    2. txt.remove(8, 1).remove(5, 2);
    3. // txt is now "some x"
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  6. #5
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parameter passing and return values of type QString/QString&

    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.

Similar Threads

  1. Carriage Return in QString
    By incapacitant in forum Newbie
    Replies: 7
    Last Post: 2nd December 2010, 09:18
  2. removing carriage return from QString
    By gren15 in forum Qt Programming
    Replies: 3
    Last Post: 28th June 2009, 22:07
  3. error passing QString ?
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2009, 12:10
  4. Replies: 4
    Last Post: 31st January 2008, 20:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.