473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3357
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: SainTiss | last post by:
Hi, Is there an extra overhead in using virtual functions and templates? Or is is just the same overhead as with regular classes? Either way, if you'd want to avoid it, will you always end up...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
2
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing...
1
by: sapropel | last post by:
ive been trying to understand Inheritance, Polymorphism and templates, and so ive tried to make a test using all of those. it is about a base class taht receives two params on the template and two...
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
4
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
2
by: glen stark | last post by:
I have a templated function template<class T> void update_E(T& updateable) { .... } What I would like to do is store a vector of updateable objects, and for each of them call the update...
5
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { //...
3
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make generic programs without templates through the use of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.