PDA

View Full Version : Can someone explain implict sharing for me?



MorrisLiang
2nd May 2010, 06:08
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?

Lykurg
2nd May 2010, 08:43
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.

tbscope
2nd May 2010, 08:47
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.

Lykurg
2nd May 2010, 09:13
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.

MorrisLiang
2nd May 2010, 10:06
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.:)

Lykurg
2nd May 2010, 11:12
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.

MorrisLiang
2nd May 2010, 12:41
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.:)

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