unnecessary declaration of Virtual destructor

L

LinuxGuy

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
 
N

Neelesh Bodas

LinuxGuy said:
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.
 
N

Neelesh Bodas

LinuxGuy said:
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.
 
L

Luke Meyers

LinuxGuy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top