Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 9th, 2008, 06:35 AM
kasthurirangan.balaji@gmail.com
Guest
 
Posts: n/a
Default Does destructor of base template class need to be virtual?

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.
  #2  
Old July 9th, 2008, 08:55 AM
=?ISO-8859-1?Q?Marcel_M=FCller?=
Guest
 
Posts: n/a
Default Re: Does destructor of base template class need to be virtual?

Hi,

kasthurirangan.balaji@gmail.com schrieb:
Quote:
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.
Quote:
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
  #3  
Old July 11th, 2008, 03:25 AM
kasthurirangan.balaji@gmail.com
Guest
 
Posts: n/a
Default Re: Does destructor of base template class need to be virtual?

On Jul 9, 2:44 am, Marcel Müller <news.5.ma...@spamgourmet.comwrote:
Quote:
Hi,
>
kasthurirangan.bal...@gmail.com schrieb:
>
Quote:
template<class Base>
class Derived : public Base
{
};
>
Quote:
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.
>
Quote:
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.
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles