The answer depends on what your FOO class does. If this is a simple structure without pointers, etc., then both approaches fit together and do the same.
Note that push_back inserts a copy of the object into the vector. If your class allocates memory on the heap, you need a copy constructor that creates a deep copy of your objects, otherwise you will get memory leaks. Also, if your objects are large enough, making copies can be inefficient. In such cases, I usually select the object itself on the heap and insert the pointer into the vector:
std::vector<FOO *> myvec; FOO *foo; foo = new FOO(); foo->init(); foo->val = 1; myvec.push_back(foo); foo = new FOO(); foo->init(); foo->val = 2; myvec.push_back(foo);
However, in this case, you need to remember the release of objects before destroying the vector.
source share