How to properly store objects into a list
-
wrote on 2 Jul 2011, 15:21 last edited by
I have a method that generates objects of a specific class; and those objects are added to a QList.
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 ?
-
wrote on 2 Jul 2011, 15:55 last edited by
If you crate objects dynamically at runtime, they'll go on the heap. else will be on the stack. (it's not all the story !)
Your first object is a list of pointers to objects of type MyClass. second one is a list of objects of type MyClass. If you overloaded assignment operator and instance constructor for MyClass, operations on your list may be time-consuming (depending on nature of your class)
bq. And how should the objects that i’m adding be created, on stack or on heap ?
AFAIK there is no general rule. It depends on what you need to implement and how your code is organized. for example if you are creating objects inside a function and planing to use them outside of it; it's a bad idea to use pointers of objects to pass them to a list outside of function. you should probably consider about passing objects by value. But in some other conditions you may want to pass object pointers.
-
wrote on 2 Jul 2011, 16:01 last edited by
If you give us more details about your class then we can give you better advise.
-
wrote on 2 Jul 2011, 16:10 last edited by
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.
-
wrote on 2 Jul 2011, 21:29 last edited by
[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.
-
wrote on 7 Jul 2011, 13:55 last edited by
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.
-
wrote on 7 Jul 2011, 14:10 last edited by
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? -
wrote on 7 Jul 2011, 14:13 last edited by
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;
}@
-
wrote on 7 Jul 2011, 14:25 last edited by
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.
-
wrote on 7 Jul 2011, 14:31 last edited by
Can you tell my why i don't want to use pointers for QString ?
-
wrote on 7 Jul 2011, 14:35 last edited by
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?
-
wrote on 7 Jul 2011, 14:40 last edited by
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)
{
}
@ -
wrote on 7 Jul 2011, 18:04 last edited by
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)
1/13