473,395 Members | 1,368 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,395 software developers and data experts.

unnecessary declaration of Virtual destructor

Hi All,

I just want to know .. In a normal class if I declare destructor as
virtual then what are the implecation to class/ processing / time.

Any help is welcome
Thanks

Rahul

Jan 6 '06 #1
4 1574
LinuxGuy wrote:
I just want to know .. In a normal class if I declare destructor as
virtual then what are the implecation to class/ processing / time.


It means, on a typical implementation, one more entry in the vtable for
that class (trivial, a minimal one-time cost per class), and an
additional pointer dereference (for vtable lookup) when the destructor
is invoked.

So, very minimal. Worth doing if there's any chance at all that
someone will eventually use it as a base class, because the resources
you'll leak if that happens and they forget to virtualize the
destructor will be a much bigger problem than the trivial additional
overhead I just described. Unless you're programming for an embedded
system or something, it may be a good idea to provide a virtual dtor
for every class rather than risk this. That, or make it impossible to
derive from your class. How to do that is left as an exercise for the
interested coder.

I assume you already know that if it's a base class (or has any virtual
member functions, which implies that it's intended to be a base class)
you should always provide a virtual dtor.

Luke

Jan 6 '06 #2
ya Thanks Luke..

Jan 6 '06 #3

LinuxGuy wrote:
Hi All,

I just want to know .. In a normal class if I declare destructor as
virtual then what are the implecation to class/ processing / time.


Depends on the way virtual functions are implemented. With typical
vtable mechanism, there is "virtually" no overhead.

Jan 7 '06 #4

LinuxGuy wrote:
Hi All,

I just want to know .. In a normal class if I declare destructor as
virtual then what are the implecation to class/ processing / time.


Depends on the way virtual functions are implemented. With typical
vtable mechanism, there is "virtually" no overhead.

Jan 7 '06 #5

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

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
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 ]
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...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
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...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.