templates vs inheritance

M

makc.the.great

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.
 
B

BigBrian

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
 
A

Arne Claus

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.
 
M

makc.the.great

Arne said:
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?
 
M

makc.the.great

BigBrian said:
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?
 
M

makc.the.great

Arne said:
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?
 
M

mlimber

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 said:
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/cujexp0310alexandr/alexandr.htm .)

Cheers! --M
 
B

ben

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
 
M

makc.the.great

ben said:
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?
 
M

makc.the.great

mlimber said:
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.
 
A

Arne Claus

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").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top