Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    How to properly store objects into a list

    General and Desktop
    5
    13
    15860
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Pufo 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> object

      And how should the objects that i'm adding be created, on stack or on heap ?

      1 Reply Last reply Reply Quote 0
      • S
        soroush 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.

        1 Reply Last reply Reply Quote 0
        • E
          Eddy last edited by

          If you give us more details about your class then we can give you better advise.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply Reply Quote 0
          • L
            ludde 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.

            1 Reply Last reply Reply Quote 0
            • G
              giesbert last edited by

              [quote author="Pufo" date="1309620091"]
              What is the difference between:
              QList<MyClass *> object
              QList<MyClass> object

              And 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.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply Reply Quote 0
              • P
                Pufo 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.

                1 Reply Last reply Reply Quote 0
                • L
                  ludde 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?

                  1 Reply Last reply Reply Quote 0
                  • P
                    Pufo 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;
                    

                    }@

                    1 Reply Last reply Reply Quote 0
                    • L
                      ludde 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.

                      1 Reply Last reply Reply Quote 0
                      • P
                        Pufo last edited by

                        Can you tell my why i don't want to use pointers for QString ?

                        1 Reply Last reply Reply Quote 0
                        • L
                          ludde 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?

                          1 Reply Last reply Reply Quote 0
                          • L
                            ludde 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)
                            {
                            }
                            @

                            1 Reply Last reply Reply Quote 0
                            • G
                              giesbert 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)

                              Nokia Certified Qt Specialist.
                              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post