Results 1 to 2 of 2

Thread: [solved]QString.replace()

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default [solved]QString.replace()

    Hi
    I have a little question.
    I have a String in my Program and this String should be immutable. But some methods have to replace something in the String and return it.
    The problem is, that QString.replace returns a reference and so the immutable String is also changed.

    Why replace returns a reference to the String instead of a proper String. So I have to copy the immutable String to another Variable and this is indricate.
    Last edited by Qiieha; 16th September 2011 at 09:25.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString.replace()

    this String should be immutable. But some methods have to replace something in the String
    So which one is it ? For me those two options are not compatible.
    So I have to copy the immutable String to another Variable and this is indricate
    Its quite straightforward :
    Qt Code:
    1. QString str = my_const_string; // no copy made here, because of implicit sharing
    2. str.replace(a,b); // my_const_string is left untouched, str gets its own copy of data and alters it
    To copy to clipboard, switch view to plain text mode 

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

    Qiieha (16th September 2011)

Similar Threads

  1. Replies: 2
    Last Post: 11th August 2011, 15:42
  2. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 20:35
  3. QString::replace() with QRegExp capture modification
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2008, 09:50
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. Replace text
    By Hz in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2006, 08:16

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.