Does destructor of base template class need to be virtual?

Discussion in 'C++' started by kasthurirangan.balaji@gmail.com, Jul 9, 2008.

  1. Guest

    Hello,

    template<class Base>
    class Derived : public Base
    {
    };

    By using template, i understand the actual base type will be deduced
    at compile time. Moreover, class Derived will consist only of
    functions which inturn call functions of the base type. Also, i would
    like to hear comments about this kind of design.

    Thanks,
    Balaji.
     
    , Jul 9, 2008
    #1
    1. Advertising

  2. Hi,

    schrieb:
    > template<class Base>
    > class Derived : public Base
    > {
    > };
    >
    > By using template, i understand the actual base type will be deduced
    > at compile time.


    true.

    However, the question from your subject is not related to that at all.
    The destructor of Base has to be virtual if and only if you delete
    objects of some derived type through ponters of type Base*. This is the
    same as for any other base class.

    > Moreover, class Derived will consist only of
    > functions which inturn call functions of the base type. Also, i would
    > like to hear comments about this kind of design.


    It is difficult to deduce what you are going to do.

    Patterns like this are somewhat uncommon, but they can be a good advise
    in some cases. Mostly, you could also use a interface of Base, a pointer
    or a reference to Base or even a member of type Base instead of this
    inheritance. In some cases where performance counts, the above solution
    is faster because it moves some logic from runtime to the compile time.
    Also it might be a lot of work to expose the whole interface of Base
    through Derived otherwise. In case it consists of public or protected
    member variables this is impossible.


    Marcel
     
    Marcel Müller, Jul 9, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 9, 2:44 am, Marcel Müller <> wrote:
    > Hi,
    >
    > schrieb:
    >
    > > template<class Base>
    > > class Derived : public Base
    > > {
    > > };

    >
    > > By using template, i understand the actual base type will be deduced
    > > at compile time.

    >
    > true.
    >
    > However, the question from your subject is not related to that at all.
    > The destructor of Base has to be virtual if and only if you delete
    > objects of some derived type through ponters of type Base*. This is the
    > same as for any other base class.
    >
    > > Moreover, class Derived will consist only of
    > > functions which inturn call functions of the base type. Also, i would
    > > like to hear comments about this kind of design.

    >
    > It is difficult to deduce what you are going to do.
    >
    > Patterns like this are somewhat uncommon, but they can be a good advise
    > in some cases. Mostly, you could also use a interface of Base, a pointer
    > or a reference to Base or even a member of type Base instead of this
    > inheritance. In some cases where performance counts, the above solution
    > is faster because it moves some logic from runtime to the compile time.
    > Also it might be a lot of work to expose the whole interface of Base
    > through Derived otherwise. In case it consists of public or protected
    > member variables this is impossible.
    >
    > Marcel


    Thanks Alf & Marcel. I shall weigh all options(inheritance,
    containment, non-template as well access thru public/protected/
    private) and also refer c++ templates.
    For further queries i shall open a new thread.

    Thanks,
    Balaji.
     
    , Jul 11, 2008
    #3
    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. Chunhui Han
    Replies:
    2
    Views:
    506
  2. qazmlp
    Replies:
    1
    Views:
    571
    qazmlp
    Apr 10, 2005
  3. frs
    Replies:
    20
    Views:
    759
    Alf P. Steinbach
    Sep 21, 2005
  4. Replies:
    2
    Views:
    564
  5. Hicham Mouline
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page