How to properly store objects into a list
-
-
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.
-
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 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.
-
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)
{
}
@ -