Say vehicle
is a base class, that has certain properties, then, inheriting from it you have say a car
, and a truck
. Then you can just do something like:
std::vector<vehicle *> parking_lot;
parking_lot.push_back(new car(x, y));
parking_lot.push_back(new truck(x1, y1));
This would be perfectly valid, and in fact very useful sometimes. The only requirement for this type of object handling is sane hierarchy of objects.
Other popular type of objects that can be used like that are e.g. people
:) you see that in almost every programming book.
EDIT:
Of course that vector can be packed with boost::shared_ptr
or std::tr1::shared_ptr
instead of raw pointers for ease of memory management. And in fact that's something I would recommend to do by all means possible.
EDIT2:
I removed a not very relevant example, here's a new one:
Say you are to implement some kind of AV scanning functionality, and you have multiple scanning engines. So you implement some kind of engine management class, say scan_manager
which can call bool scan(...)
function of those. Then you make an engine interface, say engine
. It would have a virtual bool scan(...) = 0;
Then you make a few engine
s like my_super_engine
and my_other_uber_engine
, which both inherit from engine
and implement scan(...)
. Then your engine manager would somewhere during initialization fill that std::vector<engine *>
with instances of my_super_engine
and my_other_uber_engine
and use them by calling bool scan(...)
on them either sequentially, or based on whatever types of scanning you'd like to perform. Obviously what those engines do in scan(...)
remains unknown, the only interesting bit is that bool
, so the manager can use them all in the same way without any modification.
Same can be applied to various game units, like scary_enemies
and those would be orks
, drunks
and other unpleasant creatures. They all implement void attack_good_guys(...)
and your evil_master
would make many of them and call that method.
This is indeed a common practice, and I would hardly call it bad design for as long as all those types actually are related.