PDA

View Full Version : Need an advise about adding objects to vector-



tonnot
29th August 2011, 12:19
I have a classA, it creates objectsB * (by pointer)

my_objectB = new ObjectB; ( inside classA)

And from the main, I want to add "objectsB" to a vector.

vector<objectC*> my_vector; (my vector is private for main )

So, I can write inside main_create():

for (int x=0;x<10;xx++)
{
ClassA class_a* = new ClassA;
my_vector.push_back(class_a->my_objectB)
}

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.

high_flyer
29th August 2011, 15:15
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!

stampede
29th August 2011, 16:10
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.

tonnot
29th August 2011, 18:07
Thanks.

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


for (int x=0;x<10;xx++)
{
ClassA class_a* = new ClassA;
my_vector.push_back(class_a->my_objectB)
}


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

wysota
29th August 2011, 18:10
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).