PDA

View Full Version : QList with pointers



Rockem
12th August 2009, 12:53
Is there a way to use QList with pointers like in java ?

I mean if I have somthing like that:
QList<Number *> list;
list.append(new Number(2));
list.append(new Number(4));

and get the correct answer on :
list.contains(new Number(4));

thanx
Eli

Lykurg
12th August 2009, 13:16
I mean if I have somthing like that:
QList<Number *> list;
list.append(new Number(2));
list.append(new Number(4));


No problem, works fine.



and get the correct answer on :
list.contains(new Number(4));
No, how could it work? "new" requesting for new space, so the location on your physical device (=pointer) could not be the same. If you want to check if there is a instance of Number with the value 4 you have to loop over the items. (or use any optimizations instead of a simple loop..)

wysota
12th August 2009, 13:18
The question is why would you want to use pointers in such a situation. To me Number should obviously always be allocated on stack.

Rockem
12th August 2009, 17:47
umm, I want to allocate number on the heap as it represent large object and I can have many of them on the list (also I move the list out of the scope)

so contains doesn't support pointers ? even if the object implements == ?

thanx

caduel
12th August 2009, 17:55
* QList will allocate memory on the heap internally... are you sure you want to do that yourself?
* (the list going out of scope is another matter)
* how big (in bytes) do you expect those number to be? how many do you want to store?
* if you put a pointer inside QList, you will be responsible to delete the entries (QList will not delete them in its constructor); also comparison is done on the pointer, not the object pointed to. (maybe Boost.ptr_container (http://www.boost.org/doc/libs/1_39_0/libs/ptr_container/doc/ptr_container.html) can help you, if you insist on the pointer in list thing. An alternative might be to wrap the allocated object in "smart pointers" like boost::shared_ptr

HTH

wysota
12th August 2009, 18:10
umm, I want to allocate number on the heap as it represent large object and I can have many of them on the list (also I move the list out of the scope)
It doesn't matter, it will work fine without pointers. Just make sure you implement the copy constructor and assign operator (unless the ones provided by the compiler are fine) and think about making your Number an implicitly shared class (see QSharedData).


so contains doesn't support pointers ? even if the object implements == ?

No, because the list contains pointers so it also compares pointers.

Rockem
12th August 2009, 18:31
what do you mean by "QList will allocate memory on the heap internally." ?

I mean if I do :
Number n(2);
list.append(n);
return list;

n will be deleted right ?
otherwise who will delete it

wysota
12th August 2009, 18:49
n will be deleted but its copy that is in the list will get copied again and returned as part of the list. That's why you need to implement the copy constructor for your class. And if you make Number an implicitly shared class, only the facade part of the object will be allocated on the stack and the real data will reside on heap and will not be copied whenever the object itself is copied.

Rockem
12th August 2009, 19:05
how do I make a class to be implicitly shared class ?

thanx

wysota
12th August 2009, 19:53
Open Qt Assistant and type in "Implicitly Shared Classes" in the index tab. Then type in "QSharedDataPointer".