Help on vector that holds objects and their destruction

I have a ObjectA. ObjectB and ObjectC.
ObjectB creates instances of objectC
ObjectB(){my_objectC = new ObjectC)
ObjectA creates instances of ObjectB and I'd want to save individual objectC instances inside a vector:


ObjectA:
1
2
3
4
5
6
myvector<*objectC> my_c_instances_vector;
  for (int x=0;x<10;x++; ) 
  {
  object_b = new ObjectB;         // line 1
  call object_b some methods;     // line 2 
  my_c_instances_vector.push_back(object_b->my_objectC);

}

I have some important errors related (I suposse) with construction &
destruction.

I'm saving pointers of objectsC inside my vector , ins't it?
What happens with "object_b->my_objectC" added to the vector when I create a new object_b at line 1?? Or : how can surely I add objects to my vector ?.


And... can I add objects by pointer from volatile objects ?
That is to say :
I have inside a function :
ObjectB temp_objectB;
Can I add myobjectB.objectC?
Have I to write:?
ObjectB * temp_objectB = new ObjectB
And another question, what is the life of object created 'as new' inside a function ?




By last, when I end the procces have I to delete every object of the vector manually ? How ? Assigning every vector member to a object, pop_backing and deleting the object ?

Any idea ? I need help...
Thanks



Last edited on
The STL containers make a copy of the element that you insert.
vector<*objectC> that kind of things only will be needed if you want to use polymorphism, or if the copy is too expensive (dunno if there is another way). However you may use smart pointers instead, so you don't have to worry about releasing the memory.

If you create something with new, it will live until you delete it.
By last, when I end the procces have I to delete every object of the vector manually ? How ? Assigning every vector member to a object, pop_backing and deleting the object ?
The container provides facilities to access its elements. When you access an element you are already working with it.
Topic archived. No new replies allowed.