Quote:Original post by Bregma
To tell you the truth, a good optimizing compiler (and the Microsoft one isn't too bad in that respect) will use some common optimization techniques to construct the Mesh object in place in the vector, so only one Mesh ever needs to be constructed and you don;t have to worry about pointers (and their attendant risks).
And if you are REALLY worried about it, just do Mesh.resize(Mesh.size()+1), which is ugly, but pretty much forces the new object to be created in place. Less, but still pretty likely to do that is Mesh.push_back(CMesh());
In both cases you would use Mesh.back() to get to the new object.
But as meshes are usually somewhat big (unless THEY use vectors for their data as well), it's not as critical to have them close to each other in memory for iterating and pointers would be a better choice. If this vector will have multiple pointers to the same mesh, it will be tricky to delete them later, so find a place where you manage a list with pointers to all mesh instances. (Somehow I prefer using a simple template manager class to derive stuff like mesh or texture classes from, especially since loading functions pretty much always follow the scheme "see if we loaded this file already and return pointer to existing data or load it".. and there's not much point in writing it again for all kinds of different ressources).
One thing that you should NOT do. NEVER even think about pulling a stunt like game_object.mesh=&Mesh[x], unless you can be absolutely sure that your vector never grows or has more than enough memory reserved. You don't want it to grow, be moved to a completely different address and invalidate pretty much every single mesh pointer in your program. Use the index instead, even if you dislike the small indirection.