Results 1 to 8 of 8

Thread: Can someone explain implict sharing for me?

  1. #1
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Can someone explain implict sharing for me?

    I know what implict sharing means.But I'm not sure wether I'm using it in a right way.And I read the Implicit Sharing part in the Qt document.I know only some classes is implicit sharing.

    Here are some methods declaration:
    ---------------------------------------------------------------------------------
    Group1:
    1. void parseOptions(QMap<QString, QString> options);
    2. void parseOptions(QMap<QString, QString> &options);
    3. void parseOptions(QMap<QString, QString*> options);

    Group2:
    4. void parseOptions(QMap<QString, QWidget> options);
    5. void parseOptions(QMap<QString, QWidget*> options);
    6. void parseOptions(QMap<QString, QWidget> &options);
    7. void parseOptions(QMap<QString, QWidget*> &options);

    ----------------------------------------------------------------------------------

    Question1:When I pass Container to a function,are pass-by-value and pass-by-reference the same?(1st & 2nd declaration)
    Question2:When I create a container object.Should I store values or pointers.(2nd & 3rd).
    Question3:Which one should I choose from Group2?
    It's not the goodbye that hurts,but the flashback that follow.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can someone explain implict sharing for me?

    1: there are some differences under the hood, but in normal applications they don't really matter. (prefer references, if you know, that the source will stay valid during the function call.)
    2: it depends on what type you store. for a string I would use value.
    3: this also depends on how you will use "options". If read only, use 7 with const.

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

    MorrisLiang (2nd May 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Can someone explain implict sharing for me?

    Answer to question 1:
    pass-by-value and pass-by-reference are not the same.
    If you pass the value of a container, you pass the whole container. If you only pass the reference, you only pass the reference. Always try to pass an address instead of a value especially when you need to pass a lot of data.
    Last edited by tbscope; 2nd May 2010 at 08:48. Reason: stupid laptop keyboards ;-(

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can someone explain implict sharing for me?

    Quote Originally Posted by tbscope View Post
    If you pass the value of a container, you pass the whole container.
    Not really, since QMap is also implicit shared. So under the hood no deep copy is performed only a small copy (increasing the internal instance counter) which is in speed and performance comparable to a reference.

  6. #5
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can someone explain implict sharing for me?

    Quote Originally Posted by Lykurg View Post
    1: there are some differences under the hood, but in normal applications they don't really matter. (prefer references, if you know, that the source will stay valid during the function call.)
    2: it depends on what type you store. for a string I would use value.
    3: this also depends on how you will use "options". If read only, use 7 with const.
    Thank you very much.
    It's not the goodbye that hurts,but the flashback that follow.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can someone explain implict sharing for me?

    just a note: if you use QMap<QString, QWidget*> make sure to use qDeleteAll() instead of simply call QMap::clean(). This will ensure that all memory will be freed.

  8. The following user says thank you to Lykurg for this useful post:

    MorrisLiang (2nd May 2010)

  9. #7
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can someone explain implict sharing for me?

    Quote Originally Posted by Lykurg View Post
    just a note: if you use QMap<QString, QWidget*> make sure to use qDeleteAll() instead of simply call QMap::clean(). This will ensure that all memory will be freed.
    So,qDeleteAll() will help me delete those pointers stored in a container?
    Ok,this tip is really helpful.I don't even remember I have to delete the container and its storage,because I'm new to C++.lol.
    It's not the goodbye that hurts,but the flashback that follow.

  10. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can someone explain implict sharing for me?

    qDeleteAll() and QMap::clean() is equivalent to 'while (!x.empty()) { y = x.back(); delete y; x.pop_back() }, but much more readable.

  11. The following user says thank you to squidge for this useful post:

    MorrisLiang (2nd May 2010)

Similar Threads

  1. Can't explain these errors
    By Petr_Kropotkin in forum Newbie
    Replies: 4
    Last Post: 23rd January 2010, 19:06
  2. Replies: 4
    Last Post: 17th October 2009, 22:31
  3. Explain Qt library sizes, please
    By bnilsson in forum Installation and Deployment
    Replies: 15
    Last Post: 25th July 2009, 10:01
  4. Can anyone explain the behaviour of the QtCreator
    By aarelovich in forum Qt Tools
    Replies: 5
    Last Post: 17th February 2009, 20:13
  5. could somebody please explain const to me?
    By mikro in forum General Programming
    Replies: 4
    Last Post: 29th September 2006, 14:34

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
  •  
Qt is a trademark of The Qt Company.