PDA

View Full Version : QList question



aamer4yu
2nd April 2007, 11:06
Hi.. I think this is silly question... but still

Can we use pointer to QList ?? say QList<int> * pList; ??

what are the benefits and pitfalls for using the pointer to QList ??
As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
please comment ...

Gopala Krishna
2nd April 2007, 11:48
Hi.. I think this is silly question... but still

Can we use pointer to QList ?? say QList<int> * pList; ??

what are the benefits and pitfalls for using the pointer to QList ??
As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
please comment ...

Well , since most classes in Qt are implicitly shared including QList all assignments and creation of temporary list objects involves just a pointer assignment and a variable increment, it doesn't make much difference unless you manipulate with the copied object (for eg just traversing it) . You can also use const references in relevant places to avoid these too.
But if you need serious optimizations you can use pointer to list.

aamer4yu
2nd April 2007, 12:01
what do u mean by -

since most classes in Qt are implicitly shared including QList ??

jpn
2nd April 2007, 12:20
Implicitly Shared Classes (http://doc.trolltech.com/4.2/shared.html) :)

fullmetalcoder
2nd April 2007, 13:43
Can we use pointer to QList ?? say QList<int> * pList; ??
Sure! Why couldn't we? QList is a regular C++ class... See, all classes even those commonly created/used as objects/references can be instanciated/used as pointers but the contrary isn't true (widgets for instances must be passed as pointers because a copy, even an implicit one would cause a segfault...)


what are the benefits and pitfalls for using the pointer to QList ??
As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
please comment ...
AFAIK the only advantage is when you actually want to share data between several objects. i.e. any modification done by one of the owner will be known to the others. On the contrary if you want modifications to cause forks implicitely Qt takes care of it and pointers make it tricky. As it has already been said, "return by copy" is painless and everythin with such classes is painless as long as you only use const references which can't cause deep copy.