473,854 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3393
* 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
4489
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 with code duplication? For example: let's say you've got two template specializations, which differ only slightly... So you put the common behaviour in a
5
1425
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 death) Ray
2
2177
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 information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
1
1693
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 derivies class's that print those into console or into a file. template <class T, class S> class Base{ protected: T t;
10
9645
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 instead of inheritance, and other way around? thoughts, please.
4
1498
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 the messages are different, the way to handle the message is the same. Hence I'm thinking of using templates to reduce the number of lines in the code. ------ ClassA.h ------ struct Message1;
2
735
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 function. What and how many updateable objects area actually created is determined at runtime.
5
2047
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 { // impl. };
3
3835
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 function overload and the virtual functionality. As I understand when you use templates you just increase the runtime performance since the code is resolved at compile time. How come people are mixing templates with generic programming when all...
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9751
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10682
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10758
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5743
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3187
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.