templates vs inheritance

Discussion in 'C++' started by makc.the.great@gmail.com, Oct 10, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. BigBrian Guest

    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
    BigBrian, Oct 10, 2005
    #2
    1. Advertising

  3. Arne Claus Guest

    On 2005-10-10 14:28:56 +0200, 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.
    Arne Claus, Oct 10, 2005
    #3
  4. Guest

    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, 2005
    #4
  5. Guest

    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, 2005
    #5
  6. Guest

    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, 2005
    #6
  7. mlimber Guest

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

    Cheers! --M
    mlimber, Oct 10, 2005
    #7
  8. ben Guest

    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
    ben, Oct 10, 2005
    #8
  9. Guest

    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, 2005
    #9
  10. Guest

    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, 2005
    #10
  11. Arne Claus Guest

    On 2005-10-10 15:47:06 +0200, 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").
    Arne Claus, Oct 11, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    579
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    373
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    451
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    356
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    784
    recover
    Jul 25, 2006
Loading...

Share This Page