473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Jul 9 '08 #1
2 1672
Hi,

ka*******************@gmail.com 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
Jul 9 '08 #2
On Jul 9, 2:44 am, Marcel Müller <news.5.ma...@spamgourmet.comwrote:
Hi,

kasthurirangan.bal...@gmail.com 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 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
8
by: lmfmaw | last post by:
Hi all, I've hit the wall with this "fairly" simple problem. As you can see in the code below, my destructors don't do their job as they are supposed to (I left them empty for this example). I'm...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.