How to properly store objects into a list
-
I think the choice depends very much on what MyClass is. If it's a lightweight class that is passed around by value, then put objects in the list. If it's a more complex class, e.g. something derived from QObject, you should create the objects on the heap (using new) and store pointers in the list. Of course, then you have to make sure the objects are destroyed at some point, e.g. when the list is destroyed.
-
[quote author="Pufo" date="1309620091"]
What is the difference between:
QList<MyClass *> object
QList<MyClass> objectAnd how should the objects that i'm adding be created, on stack or on heap ?
[/quote]Regarding the lists, will the second sollution create the objects on the heap while the first one will only store pointers and the objects itself can live anywhere. The content of the list will be created on the heap.
-
My class has 3 members. Two integers and a QString pointer.
I've created a list "QList<MyClass> *data" which is populated with objects ( not pointer to objects ).
If i do takeFirst() method, my program crashes.
I can avoid that if i comment out the constructor of MyClass.I can't understand this behavior.
-
I think you'll have to show us MyClass for us to be able to help you with that.
I assume you have added at least one object to the list before you do takeFirst(), right? -
I have almost 2000 objects, verified with size() method.
@#include "myline.h"
#include <QString>
#include <QChar>static QChar badArray [] = {L'þ',L'ã',L'Ã',L'ª',L'º',L'â',L'Î',L'î', L'Þ'};
static QChar goodArray [] = {'t','a','A','S','s','a','I','i','T'};MyLine::MyLine(QString &p_text, int p_startTime, int p_endTime){
text = new QString(p_text); startTime = p_startTime; endTime = p_endTime;
}
MyLine::MyLine(){
text = new QString(""); startTime = 0; endTime = 0;
}
MyLine::MyLine(const MyLine &obj){
text = obj.text; startTime = obj.startTime; endTime = obj.endTime;
}
void MyLine::addDelay(int mili){
startTime += mili; endTime += mili;
}
void MyLine::subtractDelay(int mili){
startTime -= mili; endTime -= mili;
}
void MyLine::correct(){
for (int i = 0; i < 9; i++) { if (text->indexOf(badArray[i]) != -1){ text->replace(badArray[i], goodArray[i]); } }
}
MyLine::~MyLine(){
delete text;
}@
-
You don't want to use QString that way...
Just use QString, not pointer to QString. And don't create them using new.Edit: I guess it might work, if you copy the string in the copy constructor as well. But you really don't want to use QString this way anyway.
-
Can you tell my why i don't want to use pointers for QString ?
-
QStrings are implicitly shared. You can safely pass them around as objects without worrying about how, when and where they are copied, and how much space they are taking up. I guess you can use pointers, but it just makes things much more difficult for you.
What did you do to get that error message?
-
If you use QString instead of a pointer, you can get rid of the destructor and copy constructor.
Also, you should probably consider using initializer lists in your constructors, i.e.:
@
MyLine::MyLine(const QString &p_text, int p_startTime, int p_endTime)
: text(p_text)
, startTime(p_startTime)
, endTime(p_endTime)
{
}
@ -
As a side node, the crash comes from here:
@
MyLine::MyLine(const MyLine &obj){text = obj.text; startTime = obj.startTime; endTime = obj.endTime;
}
@You copy the pointer. and in the destructor of both objects, you then destroy it (this and obj)