Quote:Original post by thedevdan
It is becoming painfully apparent that I can't use vectors on actual data; that I will need to use vectors of pointers. This is because I will be using interfaces extensively. Should I use boost's shared_ptr? What other options are there? Surely they don't manage memory all themselves?
Since nobody has brought this up yet, I figured I should mention it. Unless you have containers(vector, list, etc) of simple intrinsic types(int, float, etc) you should almost always prefer containers that hold pointers to your objects instead of containers of actual objects. The reason for this is that the STL makes a lot of copies of whatever the containers hold. It makes a copy when you pass something in, it makes a copy when you get something out, and it may make copies at other times, like when it re-sizes itself.
Now, whether you should use a container of raw pointers or a container of something like a shared_ptr has less to do with the container itself and more to do with how you intend to manage the data it holds. If you really feel that you need to be able to create/destroy this data in an ad-hoc manner any place in your application, then shared_ptr may be the way to go. If that's not the case, then raw pointers should be fine.
With that being said, whenever I need to access data at many places in my application I personally prefer the approach of having some kind of class that holds and manages this data for me. The other parts of the application can ask to look at the data, but they are not allowed to create/drestroy it directly. I think this makes it much easier to track down problems because it all happens in one place, instead of being scattered about your application.
One last reminder, make sure your classes destructors are virtual. Otherwise, as I'm sure you know, you'll run into problems if you go to delete pointers to your base class.
-John