Mail Archives: djgpp/2002/06/14/13:50:22
Jason wrote:
>
> So, when I push an instance of my class onto a vector container, is it just
> creating a pointer to a global class instance pointer? If I create a series
> of class instances manually (ie, CBoard Board1; CBoard Board2; ; ; CBoard
> Board x) a totally independant class group is created, and there is no
> dramas. I might have to look a bit further into what goes on when I push a
> class instance onto a vector container (as this is what I want, without the
> confusing code!) I just want to be able to have a run-time-defined number
> of a particular class, where I can access member functions of each class
> instance independantly like an array.
>
> Jase.
No, vectors do not store pointers unless you push pointers. If you push on a raw instance, ie:
class foo;
vector<foo> v;
foo f;
v.push(f);
a new foo will be created (using the copy constructor, foo(foo&), based on f), and pushed on the
vector (not a pointer - the entire object). If f contains a pointer to a memory area, and foo's copy
constructor does not create a new memory area and copy to there, then both objects (f and the one in
the vector) will have the same pointer internally. When one is destroyed, and it frees that area,
the other will be referencing a freed memory region - very bad. Just give your objects proper copy
constructors, and it should work fine.
- Raw text -