Results 1 to 5 of 5

Thread: Need an advise about adding objects to vector-

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Need an advise about adding objects to vector-

    I have a classA, it creates objectsB * (by pointer)
    Qt Code:
    1. my_objectB = new ObjectB; ( inside classA)
    To copy to clipboard, switch view to plain text mode 

    And from the main, I want to add "objectsB" to a vector.
    Qt Code:
    1. vector<objectC*> my_vector; (my vector is private for main )
    To copy to clipboard, switch view to plain text mode 

    So, I can write inside main_create():
    Qt Code:
    1. for (int x=0;x<10;xx++)
    2. {
    3. ClassA class_a* = new ClassA;
    4. my_vector.push_back(class_a->my_objectB)
    5. }
    To copy to clipboard, switch view to plain text mode 

    My doubt is ... Every time I create class_a, a new instance is created, and there is no re-write of class_a->my_objectB, ins't it ?

    Ok. on main I have main_process, a function that are going to do thing with the vector. My question is , objectB instances inside the vector exist ? or maybe I have a risk of crash becasue thet were created by a temporal classA inside 'main_create'.

    By last. I have to free every objectB instance inside the vector with pop_back ? Or need I to do more ?
    Any tip ? Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need an advise about adding objects to vector-

    And from the main, I want to add "objectsB" to a vector.
    This all sounds very confused.

    And from the main, I want to add "objectsB" to a vector.
    Let me see if I understand:
    You have a vector in main.
    And you want to add a member of ClassA (objectB) to that vector - is that correct?
    If this is correct, then you are doing something very wrong!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need an advise about adding objects to vector-

    Quote Originally Posted by tonnot View Post
    Every time I create class_a, a new instance is created, and there is no re-write of class_a->my_objectB, ins't it ?
    No, as long my_objectB is not a static member of the class.
    objectB instances inside the vector exist ?
    If they were created on the heap (using operator new), they will exist until you call "delete" on them.
    I have to free every objectB instance inside the vector with pop_back ? Or need I to do more ?
    You need to call "delete" on the object to "free" it, pop_back will just remove the pointer from vector, it will not release the associated memory.

    Any tip ?
    I think you should do some redesign. For example, I have a feeling that at some point you want to delete each object from the vector. This is confusing, because if they all are a members of some ClassA, then it's more "natural" to think that the ClassA instance "owns" instance of ClassB, so it should be responsible for deleting them. If you explicitly delete instance from vector, then you will have a "dangling" pointer in ClassA objects.
    Maybe you should use some kind of factory method for creating objects of ClassB instead of storing pointers to members. Or store pointers to ClassA in your vector and make this class responsible for ClassB objects.
    Btw if the code snippet (the for loop) is unchanged in your actual code, then probably you have a memory leak - you do not "delete" the ClassA objects.

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need an advise about adding objects to vector-

    Thanks.

    Ok. the post are a initial skeleton.
    My main doubts are related with the inner objects.

    Qt Code:
    1. for (int x=0;x<10;xx++)
    2. {
    3. ClassA class_a* = new ClassA;
    4. my_vector.push_back(class_a->my_objectB)
    5. }
    To copy to clipboard, switch view to plain text mode 

    For every x I create a 'volatil' A ???
    class_a is automatically deleted at the end of the loop???
    Thanks

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need an advise about adding objects to vector-

    No, nothing is deleted. I suggest you spend some time reading "Thinking in C++", it deals with problems such as your current one (ownership and stuff).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    tonnot (29th August 2011)

Similar Threads

  1. UI like Guitar Pro 6 - Please advise me.
    By jeanremi in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2011, 08:26
  2. Advise:Do I need QDataWidgetMapper?
    By fatecasino in forum Newbie
    Replies: 1
    Last Post: 4th March 2011, 01:04
  3. Replies: 11
    Last Post: 25th February 2009, 17:35
  4. Replies: 7
    Last Post: 18th July 2006, 21:33
  5. vector of objects
    By mickey in forum General Programming
    Replies: 2
    Last Post: 8th May 2006, 20:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.