I'm still pretty new to c++ (coming over from java).
I have a stl list of type Actor
. When Actor
only contained "real" methods there was no problem. I now want to extend this class to several classes, and have a need to change some methods to be abstract, since they don't make sense as concrete anymore.
As I expected (from the documentation) this is bad news because you can no longer instantiate Actor
, and so when I iterate through my list I run into problems.
What is the c++ way to do this?
Sorry if there's anything unclear
You can not handle this directly:
As you can see when the class is abstract you can not instanciate the object.
Even if the class where not abstract you would not be able to put derived objects into the list because of the slicing problem.
The solution is to use pointers.
So the first question is who owns the pointer (it is the responcability of the owner to delete it when its life time is over).
With a std::list<> the list took ownership by creating a copy of the object and taking ownership of the copy. But the destructor of a pointer does nothing. You need to manually call the delete on a pointer to get the destructor of the obejct to activate. So std::list<> is not a good option for holding pointers when it also needs to take ownership.
// Objects are owned by the scope, the list just holds apointer.
ActorD1 a1; Actor D1 derived from Actor
ActorD2 a2;
ActorD2 a3;
std::list<Actor*> actorList;
actorList.push_back(&a1);
actorList.push_back(&a2);
actorList.push_back(&a3);
This works fine as the list will go out of scope then the objects everything works fine. But this is not very useful for dynamically (run-time) created objects.
Boost provides a set of containers that handle pointers. You give ownership of the pointer to the container and the object is destroyed by the containter when the container goes out ofd scope.
boost::ptr_list<Actor> actorList;
actorList.push_back(new ActorD1);
actorList.push_back(new ActorD2);
actorList.push_back(new ActorD2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With