quote: Original post by Flarelocke
I''m a bit confused by the code you posted. By using a priority queue of orders, you seem to indicate that the order is a class, but by the example order(move), you seem to indicate that the order is simply a function call.
The priority queue of orders is data private to the orderable object. When an order is issued (and this is why I extended the move method to take an order_issuer object), it is inserted into this queue along with other data. The orderable object thus uses some set of discriminants to determine which order is the most "important" and executes that first. The definition of important has to be able to change, thus wonderfully complicating the commander-commanded interaction without affecting the code.
A company of infantrymen may trust their own luitenant or captain more than an external corporal or other newly-inserted commander - particularly one who has proved his ineptitude (see Band of Brothers for one example of this) - and as such may take the lower-ranked officer''s commands as being more important. Alternatively, events around them may cause certain orders - or even fundamental instincts - to cause them to disregard protocol. That way, your soldiers wont stand in the path of heavy fire and be mowed down like sheep when they know they don''t have a chance. They''ll break formation and seek cover instead (given the appropriate instincts and AI routines).
In effect, there is an order class (which stores details about a particular order) and a set of order-generating algorithms (which operate on all objects that support orderable interfaces - which may or may not be an inheritance scheme).
<aside>
One of the interesting advantages of templates is that it makes it possible to "share" interfaces between completely unrelated classes. If you wished to write an algorithm that operated on arbitrary objects that provided a method doSomething without using templates, you''d need to derive them all from a single base class that provides the virtual doSomething signature and then operate on a pointer to said base class which will actually reference one of the derived classes, thus creating an artificial relationship between your types. None of the derived classes ISA base.
Using templates, however, the method signature only needs to be present in the class for which the template is being instantiated. This is what generic programming enthusiasts and apologists call conforming to a concept. Incidentally, this is what the STL is based on, as std::vector, std::string and so forth obviously don''t inherit from the same classes in all cases. Their iterators do, however, which reinforces the fact/argument that you can use inheritance with templates rather than instead of templates.
</aside>
quote:
I don''t know anything about the runtime costs of polymorphism, so I can''t downplay their significance
They''re not that significant. An array access (dereference), a pointer dereference and you''re pretty much done in most compiler implementations.