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?
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.
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.
Re: Can someone explain implict sharing for me?
Quote:
Originally Posted by
tbscope
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.
Re: Can someone explain implict sharing for me?
Quote:
Originally Posted by
Lykurg
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.:)
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.
Re: Can someone explain implict sharing for me?
Quote:
Originally Posted by
Lykurg
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.:)
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.