Templates and Polymorphism

S

sarathy

Hi all,
I need a small clarification reg. Templates and
Polymorphism. I believe templates is really a good feature, which can
be used to implement generic functions and classes. But i doubt whether
it should not be used in certain cases.

Consider the case when all the params to a template
function/class are similar. My questions is that whatever can be
acheived by a template in such a case, can be acheived by runtime
polymorphism. Since polymorphism is a core OO concept, i think one
should go with polymorphism rather than templates.

I came accross several examples, quoting that a generic
sort can be coded using templates. But if that is the case, make a sort
method with the polymorhic base class as args and pass polymorphic
types (base and all derived ) as args to this method. I mean to say
that.

Array

-------------------------------------------------------------------------------------
| | |
|
Int Array Float Array Double Array
Long Array


sort (Array , size);
sort (FloatArray , size);
sort (IntArray , size);
sort (DoubleArray , size);
sort (LongArray , size);

In this case, is a polymorphism recommended, or a template recommended.
Please do clarify.

Regards,
Sarathy
 
A

Alf P. Steinbach

* sarathy:
Hi all,
I need a small clarification reg. Templates and
Polymorphism. I believe templates is really a good feature, which can
be used to implement generic functions and classes. But i doubt whether
it should not be used in certain cases.

Consider the case when all the params to a template
function/class are similar. My questions is that whatever can be
acheived by a template in such a case, can be acheived by runtime
polymorphism.

That's not quite accurate. Probably you mean, "whatever /external
effect/ can be achieved...". Templates provide static type checking,
not external effect.

Whatever external effect can be achieved in C++, can also be achieved
using C or assembly language.

Perhaps considering what the advantages of C++ are, compared to these
languages that can do the same wrt. external effet, can help clarify?

Since polymorphism is a core OO concept, i think one
should go with polymorphism rather than templates.

I came accross several examples, quoting that a generic
sort can be coded using templates. But if that is the case, make a sort
method with the polymorhic base class as args and pass polymorphic
types (base and all derived ) as args to this method.

This is possible but very awkward and inefficient, and it introduces the
dreaded Universal Base Class.

I mean to say
that.

Array

-------------------------------------------------------------------------------------
| | |
|
Int Array Float Array Double Array
Long Array


sort (Array , size);
sort (FloatArray , size);
sort (IntArray , size);
sort (DoubleArray , size);
sort (LongArray , size);

In this case, is a polymorphism recommended, or a template recommended.

Is std::sort based on run-time polymorphism, or compile-time?

Are built-in types polymorphic?
 
A

Alan Johnson

sarathy said:
Hi all,
I need a small clarification reg. Templates and
Polymorphism. I believe templates is really a good feature, which can
be used to implement generic functions and classes. But i doubt whether
it should not be used in certain cases.

Consider the case when all the params to a template
function/class are similar. My questions is that whatever can be
acheived by a template in such a case, can be acheived by runtime
polymorphism. Since polymorphism is a core OO concept, i think one
should go with polymorphism rather than templates.

I came accross several examples, quoting that a generic
sort can be coded using templates. But if that is the case, make a sort
method with the polymorhic base class as args and pass polymorphic
types (base and all derived ) as args to this method. I mean to say
that.

Array

-------------------------------------------------------------------------------------
| | |
|
Int Array Float Array Double Array
Long Array


sort (Array , size);
sort (FloatArray , size);
sort (IntArray , size);
sort (DoubleArray , size);
sort (LongArray , size);

In this case, is a polymorphism recommended, or a template recommended.
Please do clarify.

Templates (compile-time polymorphism) are no less polymorphic than
virtual function dispatching (run-time polymorphism). That is, in both
cases you have a single interface for multiple types of objects.

It is true that run-time polymorphism is generally more flexible. In
particular, you can treat objects whose type is not known until run-time
polymorphically. That flexibility generally comes at a cost. In most
implementations an abstract class will have a vtable (costing additional
memory), and virtual function calls will require a pointer dereference
or two (costing additional time). Symmetrically, compile-time
polymorphism will generally have additional compile time cost.

Often, though, you do not need the flexibility of run-time polymorphism.
If your goal is to create a sort that can sort objects of statically
known type (as is typically the case), then there is no point in using
run-time polymorphism. If your goal is to create a sort that can sort
objects whose type will not be known until run-time, then run-time
polymorphism is appropriate.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top