Results 1 to 3 of 3

Thread: returning a string

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default returning a string

    Hello,
    I have inside a method to manipulate a string large about 500 digits; I need to return it out; what's better? return it as reference parameter or as returned value? Or maybe keep it as member of the class?

    thanks.
    Regards

  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: returning a string

    It depends whether you have to copy the data when you make a copy of the object. If not (like with QString), you can return by value. Otherwise a reference or a const reference would be advised. Keeping it as a class member won't change anything because at some point you have to access the member somehow - if you access it directly, it's like you'd return a reference. If the member is private, you'll be facing your original question again.

  3. #3
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: returning a string

    Quote Originally Posted by wysota View Post
    It depends whether you have to copy the data when you make a copy of the object. If not (like with QString), you can return by value. Otherwise a reference or a const reference would be advised. Keeping it as a class member won't change anything because at some point you have to access the member somehow - if you access it directly, it's like you'd return a reference. If the member is private, you'll be facing your original question again.
    But do not forget that you should not return a reference to an object which is instanciated inside the method. As soon as the method is left, the object for which you returned the reference is destroyed and the reference becomes invalid.
    There are 10 people in the world. Those who understand binary and those who don't.

Similar Threads

  1. Replies: 5
    Last Post: 10th January 2008, 13:26
  2. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  3. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  4. string to char*?
    By nass in forum General Programming
    Replies: 3
    Last Post: 2nd January 2007, 15:47
  5. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38

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.