This code will explode, right? Once the loop exits, the original instances will die with all their internal elements, so if they are not do_stuff , any method, such as do_stuff that requires access to members of B , will throw a segmentation error, right?
void foo() { std::vector<B> bar; for (int i = 0; i < 7; i++) bar.push_back(B(i, i, i)); bar[3].do_stuff(); }
So, is there a way to do this without using a pointer? Or you need to do this:
void foo() { std::vector<B*> bar; for (int i = 0; i < 7; i++) bar.push_back(new B(i, i, i)); bar[3]->do_stuff(); for (int i = 0; i < 7; i++) delete bar[i]; }
c ++
zehelvion
source share