static vs. dynamic polymporhism

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
 
C

Cy Edmunds

Mark P said:
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

It has been my experience that relatively small, self-contained functions
and classes (e.g.standard library <algorithms>) are best coded as templates.
Large, complex highly coupled frameworks (e.g.GUI software) are best coded
using abstract base classes. Your code sounds more like the former case to
me.

Cy
 
D

David White

Mark said:
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.

What are the template's parameters?
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.

The two approaches have such different properties that I'd expect the choice between them
to be pretty clear almost always. A class template can only be used when all variations of
it are known at compile time, and it can only generate classes that are incompatible with
each other (i.e., one variation of the template cannot be used where another is expected),
except when they have a common base class, but then you are back to run-time polymorphism.
If you know all your types at any point in your code at compile time, you don't need
run-time polymorphism. I think I'd need an explicit example (with actual code) where you
have a choice between the two to understand what you are getting at.

DW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top