468,456 Members | 1,780 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Templates and Polymorphism

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

Jul 31 '06 #1
2 3016
* 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?

Please do clarify.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 31 '06 #2
sarathy wrote:
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.

--
Alan Johnson
Jul 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by SainTiss | last post: by
5 posts views Thread by Ray Gardener | last post: by
2 posts views Thread by kelvSYC | last post: by
10 posts views Thread by makc.the.great | last post: by
4 posts views Thread by qning88 | last post: by
2 posts views Thread by glen stark | last post: by
5 posts views Thread by Zeppe | last post: by
3 posts views Thread by Johs | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.