By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,668 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,668 IT Pros & Developers. It's quick & easy.

Templates and Polymorphism

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
* 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

P: n/a
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.