I will like to create a struct that holds a higher state for other structs.
I tried to create it like this, but I recently found that returning a pointer to an element of an std::vector is not safe, since that pointer can change.
struct Foo {
std::string &context;
std::string content;
Foo(std::string &context, std::string content) : context(context), content(content) {}
}
struct Bar {
std::string context;
std::vector<std::string> manyFoos;
Foo* addFoo(std::string content) {
manyFoos.emplace_back(context, content);
return &manyFoos[manyFoos.size() - 1];
}
}
struct Example {
Bar bar;
Foo* fooA;
Foo* fooB;
Example() {
fooA = bar.addFoo("Hello");
fooB = bar.addFoo("World");
}
}
What could be a good and safe way of doing this?
std::list<T>for example