471,073 Members | 1,308 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 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 9381

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Markus Seeger | last post: by
3 posts views Thread by darkstorm | last post: by
11 posts views Thread by Peter Oliphant | last post: by
4 posts views Thread by qning88 | last post: by
6 posts views Thread by Ravi Rao | last post: by
2 posts views Thread by shashwatgaur | last post: by
2 posts views Thread by Isaac Gelado | last post: by
reply views Thread by leo001 | last post: by

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.