M
Mark P
Still being relatively new to C++ I like to go back to review and
rethink old designs to see where I could do things better. The issue
I'm currently pondering is static vs. dynamic polymorphism, or
equivalently in my case, templates vs abstract base classes.
Let me be concrete. I have a class for performing a geometry operation
which takes a collection of line segments and breaks them into minimal
nonintersecting subsegments. I wrote this using templates. There is a
templated Edge class to represent line segments-- this includes
accessors for various edge properties (end points, direction, etc.) as
well as a facility for breaking of a piece of the edge. There is a list
of 8 simple functions which an Edge class must implement in order for
the template code to compile.
Similarly I have a templated "Receiver" class which is essentially a
callback object to receive the output of the segmentation computation.
There are 2 functions which a Receiver class must implement in order to
work with the computational engine.
So now to my question: what are the merits and/or faults of using
templates rather than an abstract base class? To my mind I don't see
much to be gained by using an abstract base class with virtual functions
except that the code may look a little cleaner without angle brackets
and long names. In fact, my current way of thinking is that templates
should be preferred to abstract base classes unless there is a specific
need for run-time polymorphism. (And even then an abstract base class
can be used as the template parameter, so nothing is sacrificed by
beginning with a template approach).
But have I gone too far in this last statement and is there an
appropriate usage of ABCs beyond run-time polymorhpism? One issue that
comes to mind is code bloat-- namely, is more code generated by three
instantiations of a template class compared to one instantiation of a
non-template class used in three different instances and with three
different types derived from an ABC? More generally, I wonder what else
I'm overlooking in contrasting these two approaches. I'd really
appreciate any insight on this topic.
Thanks,
Mark
rethink old designs to see where I could do things better. The issue
I'm currently pondering is static vs. dynamic polymorphism, or
equivalently in my case, templates vs abstract base classes.
Let me be concrete. I have a class for performing a geometry operation
which takes a collection of line segments and breaks them into minimal
nonintersecting subsegments. I wrote this using templates. There is a
templated Edge class to represent line segments-- this includes
accessors for various edge properties (end points, direction, etc.) as
well as a facility for breaking of a piece of the edge. There is a list
of 8 simple functions which an Edge class must implement in order for
the template code to compile.
Similarly I have a templated "Receiver" class which is essentially a
callback object to receive the output of the segmentation computation.
There are 2 functions which a Receiver class must implement in order to
work with the computational engine.
So now to my question: what are the merits and/or faults of using
templates rather than an abstract base class? To my mind I don't see
much to be gained by using an abstract base class with virtual functions
except that the code may look a little cleaner without angle brackets
and long names. In fact, my current way of thinking is that templates
should be preferred to abstract base classes unless there is a specific
need for run-time polymorphism. (And even then an abstract base class
can be used as the template parameter, so nothing is sacrificed by
beginning with a template approach).
But have I gone too far in this last statement and is there an
appropriate usage of ABCs beyond run-time polymorhpism? One issue that
comes to mind is code bloat-- namely, is more code generated by three
instantiations of a template class compared to one instantiation of a
non-template class used in three different instances and with three
different types derived from an ABC? More generally, I wonder what else
I'm overlooking in contrasting these two approaches. I'd really
appreciate any insight on this topic.
Thanks,
Mark