Vector of pointers to objects stored in a vector
So I'm working on a program, and I wanted to do something like this:
Class A has a vector<object>, where all the objects of that type are stored in. Each time a new object is created, I push it back there.
Class B has a vector<object*>, so each time a new object is created on class A, I wanted to push back a pointer to that object here.
Basically something like this:
Code:
Class A
{
...
vector<object> vec;
vector<B> classes;
};
Class B
{
...
vector<object*> vec;
};
A::vec.push_back (something);
A::classes[some index].add (&A::vec[A::vec.size()-1]);
So the object would be on class A vector, and one of the classes B would have a pointer to that object, on a vector of pointers.
I know I could just not use a vector<object> on class A, and simply use new/delete and add it as a pointer to class B, but in this case I'd like to do it this way, and it's just not working. Each time I push back a new pointer on class B, all the other pointers become invalid.
Note: The code is just there as an example, it's obviously not actual code.
Any help appreciated!