473,395 Members | 1,996 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,395 software developers and data experts.

templates vs inheritance

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.

Oct 10 '05 #1
10 9595

ma************@gmail.com wrote:
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.


What "effect" are you refering to? Without knowing exactly what you're
refering to, this may not apply, but....

Templates are instanciated at compile time. If you're using
inheritance and ploymorphism, the dynamic binding is done at run time.
Thus the "effect" is not the same.

-Brian

Oct 10 '05 #2
On 2005-10-10 14:28:56 +0200, ma************@gmail.com said:
now that I am looking at templates, there's the question.

why same effect couldn't/shouldn't be achieved with inheritance?


Because you can't.
Imagine a 3d-vector class for ints and floats. Both have exactly the
same operations, just a different datatype. So as writing two variants
seems a bad idea (double work = doubled amount of mistakes + code
management problems) and it would be nice to have a "generic" 3d-vector.
The only way to create a "generic" vector via inheritance is to create
a wrapperclass for basic datatypes, derive a float and int and just
allow your vector class to use that wrapper.
And as you can see in e.g. Java - that's slow (and not really "handy").

So the easier way is to create a vector with a datatype "x" where "x"
can be replaced with any datatype providing the functions you use (e.g.
+ and -). That's what templates are for.

Oct 10 '05 #3
Arne Claus wrote:
Because you can't.
Imagine a 3d-vector class for ints and floats. Both have exactly the
same operations, just a different datatype.


can't you have CAbstractDataType, and then derive CInt and CFloat from
that?

Oct 10 '05 #4
BigBrian wrote:
What "effect" are you refering to?


an "effect" of applying same code to different data types. it seems (at
1st) that one can go any way about it. so. there are two hammers, there
must be a reason why. what kind of nails I use each hammer for?

Oct 10 '05 #5
Arne Claus wrote:
And as you can see in e.g. Java - that's slow But I don't see it in C++ (and not really "handy").
So the easier way is to create a vector with a datatype "x" where "x"
can be replaced with any datatype providing the functions you use (e.g.
+ and -). That's what templates are for.

So, your vision is "handiness". Ok. I just feel there must be more to
it?

Oct 10 '05 #6
ma************@gmail.com wrote:
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.
Templates allows a variety of interesting and useful constructs that
are not available (or are *really* inconvenient) with inheritance
alone. See the Boost libraries and _Modern C++ Design_ for some good
examples. For instance, you can create a policy-based design using
templates, such as this policy-based smart pointer from the Loki
library:

template
<
typename T,
template <class> class OwnershipPolicy,
class ConversionPolicy,
template <class> class CheckingPolicy,
template <class> class StoragePolicy

class SmartPtr
: public StoragePolicy<T>
, public OwnershipPolicy<typename StoragePolicy<T>::PointerType>
, public CheckingPolicy<typename StoragePolicy<T>::StoredType>
, public ConversionPolicy
{
// use policy members here
};

(See also the improvements to the original design in the CUJ artiles
starting with
http://www.cuj.com/documents/s=8890/...r/alexandr.htm .)

Cheers! --M

Oct 10 '05 #7
ben
ma************@gmail.com wrote:
BigBrian wrote:
What "effect" are you refering to?

an "effect" of applying same code to different data types. it seems (at
1st) that one can go any way about it. so. there are two hammers, there
must be a reason why. what kind of nails I use each hammer for?


You are referring to polymorphism. Virtual function achieves dynamic
polymorphism at runtime while template achieves static polymorphism at
compile time.

Generally, the compiler needs to know the exact type information at
compile time in order to achieve static polymorphism. For situations
where the exact type of an object can only be known at runtime, template
is helpless.

Usually, templates and virtual functions are very different and you
should be able to use both seamlessly. A good example would be:

template <typename DimensionType>
class shape
{
public:
virtual DimensionType get_area(void)=0;
// ...
};

template <typename DimensionType>
class circle: public shape<DimensionType>
{
public:
DimensionType radius(void);

DimensionType get_area(void)
{
return 3.14*radius()*radius();
}

// ...
};

Notice the different levels of code reuse.

Ben
Oct 10 '05 #8
ben wrote:
For situations
where the exact type of an object can only be known at runtime, template
is helpless.


Now, that's interesting. What are these situations? Typical examples,
perhaps?

Oct 11 '05 #9
mlimber wrote:
Templates allows a variety of interesting and useful constructs that
are not available (or are *really* inconvenient) with inheritance
alone.


See somebody's previous message on incovenience - this point was
already made. Basically, it's about doing thins differently. If you are
trying to do it the *same* way templates allow you to do it, than - yes
- it is inconvenient.

Oct 11 '05 #10
On 2005-10-10 15:47:06 +0200, ma************@gmail.com said:
Arne Claus wrote:
Because you can't.
Imagine a 3d-vector class for ints and floats. Both have exactly the
same operations, just a different datatype.


can't you have CAbstractDataType, and then derive CInt and CFloat from
that?


Yes, but as I said this is slow (big reason) and somewhat akward to use.
But this was example is just one of many. Take the STL for example
where you have e.g. a template for a linear list. Would you like to be
forced to derive every datatype you want to put in that list from an
CObject class? I guess not. In Java this would happen if all your
classes won't be automatically derived from java.lang.object (and java
has introduced templates, too with the last version because it makes
things much easier even with that inheritance "trick").

Oct 11 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
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...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: shashwatgaur | last post by:
Can some one tell me what are the differenc b/w templates and inheritance. I know one that templates are instantiated at compile time while inheritance is a result of run time.
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.