How to use QList<*> * object with [] operator ?
-
operator. I have been able to use this feature when I had an object like QList<pToObject *> myList. But instead, when I have a pointer to such a QList I can't seem to get it done.
Consider the next code:
@#include <QApplication>
#include <QList>struct test{
int a;
int b;
};int main(int argc, char *argv[]){
QApplication app(argc, argv);// QList<pToObject * > * pToMyList;
QList<test *> * values;
for (int i=0; i<5; i++){
test * ps = new test;
ps->a = i;
ps->b = i+1;
values->append(ps);
}qDebug("%d", values[2]->a);
return app.exec();
}@
When I run the code above I run into:
@main.cpp 21: error: base operand of '->' has non-pointer type 'QList<test*>'@
operator when dealing with a pointer. Dereferencing the object ( *values[ 2 ]->a ) throws an error as well.
Could someone please show me the way?p.s. I know I could use QList.at() but this doesn't allow me to write on the object. QList.takeAt() is not usable since it detaches the item from the QList, while instead I need to make changes but still leave the object in the QList.
-
Hi,
[quote]when I have a pointer to such a QList I can’t seem to get it done.[/quote]That's because [] performs arithmetic on pointers.
@
// Initialize a char pointer:
char* string = ...char c;
// The following two lines are equivalent:
c = string[5];
c = *(string+5);
@So, in your code,
@values[2]@
is like calling
@*(values+2)@What is your goal for using a pointer?
-
To Arnaut: my mistake, I forgot the initialization part which I have corrected while writing this post (but I didn't update the post... :D). Anyway, thank you very much, that works just fine!
To: JKSH: my goal is to store many struct pointers in a class. so basically I need a QList pointer in my class header before I can use it:
@ struct Obj{ ... };
class myclass{
...
private:
QList<Obj * > * pMyList;
}@
During execution I load objects in pMyList using append(). But I often need to modify the values inside the saved structs, so I can't use at() and takeAt() for the reasons explained before.
Do you think there is a better alternative than Arnaut's one? -
Hi,
What JKSH meant is why is pMyList a pointer ? There's no need for that, just use
@QList<Obj *> myList;@
-
[quote author="T3STY" date="1392118794"]I'm trying to prevent stack overflow problems since I know the QList is going to be pretty heavy... although, I read now in the docs that QList stores items as pointers to data so it wouldn't be such a big issue...[/quote]All of Qt's containers (QList, QString, QMap, etc.) take up a very tiny amount of stack space. All of their data are stored on the heap.
Like SGaist said, there is no need to use pointers to QLists.
Some people use pointers to -share a list between two objects- avoid copying data when letting two objects read the same list. This is not required (and should be avoided), because copying a QList is very cheap too. The data is "implicitly shared":http://qt-project.org/doc/qt-5/implicit-sharing.html . Anyway, const references should be used for this purpose.
-
[quote author="JKSH" date="1392119103"]Some people use pointers to share a list between two objects. This is not required (and should be avoided), because copying a QList is very cheap too. The data is "implicitly shared":http://qt-project.org/doc/qt-5/implicit-sharing.html[/quote]
One should be aware though that copy-on-write will make these independent objects as soon as you modify one of the lists, at which point the two lists will be distinct and different. This is a fundamental difference to using pointers to a shared list.
-
Thanks mmol. I shouldn't have said "share" -- I meant "avoid copying data"