Saturday, 17 August 2013

Keeping things in a vector invalidates internal references, how to solve?

Keeping things in a vector invalidates internal references, how to solve?

I have a struct like this:
struct A {
B b;
C c;
}
Where c keeps a reference to b. I keep A's in a vector:
std::vector<A> as;
When pushing back new elements into the vector, it may move in memory.
This changes the adress of the b's and invalidates the reference that c
has to b. Is there any better way to solve this than to move the data of b
out of the struct and keep a pointer to it?

No comments:

Post a Comment