By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Question regarding virtual functions/destructors

P: n/a
Hello All,

I had some query regarding virtual functions/destructors. If a class is
having some/all of its methods that are virtual then is it recommended
that it should also have virtual destructor?

When I am defining such a class with default destructor then my
compiler is giving warning that class XXX has virtual functions but
non-virtual destructor.

Please guide me in this regard. Thanks in advance.

Regards,
Pravesh

Jun 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Pravesh:
I had some query regarding virtual functions/destructors. If a class is
having some/all of its methods that are virtual then is it recommended
that it should also have virtual destructor?
Yes. A virtual destructor is required for deleting an instance of the
class through a base class pointer. To be safe you should support that.
When I am defining such a class with default destructor then my
compiler is giving warning that class XXX has virtual functions but
non-virtual destructor.

Please guide me in this regard. Thanks in advance.


Just make the destructor virtual.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 20 '06 #2

P: n/a
Pravesh wrote:
Hello All,

I had some query regarding virtual functions/destructors. If a class is
having some/all of its methods that are virtual then is it recommended
that it should also have virtual destructor?
Incorrect. When you are deleting a derived object D through a base
class pointer B (e.g. the object is of type D, but you are invoking
delete on a (B *), that base class must have a virtual destructor. If
it does not, then it essentially amounts to an attempt to destroy and
delete the object as if it were a B without any D part. The behavior is
undefined then. Not only might the D destructor fail to run, the
operation can cause the program to terminate or behave erratically.
When I am defining such a class with default destructor then my
compiler is giving warning that class XXX has virtual functions but
non-virtual destructor.


The reason the compiler says that is that a class with virtual
functions is almost certainly designed for inheritance, and it's almost
certain that the program will use virtual functions as interfaces to a
derived class through base class references or pointers. Such programs
quite often end up with memory management involving base classes: the
end of an object's lifetime is computed at a point where its exact type
is not know, only a base class.

But that destructor problem could happen without virtual functions. But
the compiler can't warn about those cases without becoming a nuisance.
The compiler would have to gather information about how a class is
used; notice that there is a D derived from B, and that B has no
virtual destructor, and that there are places in the code where a B *
pointer is being deleted. If the compiler could notice all three things
in one compilation pass, it could emit a warning like ``B is being used
for inheritance, pointers to B are being deleted, yet it has a
non-virtual destructor''.

Jun 20 '06 #3

P: n/a

Kaz Kylheku wrote:
Pravesh wrote:
Hello All,

I had some query regarding virtual functions/destructors. If a class is
having some/all of its methods that are virtual then is it recommended
that it should also have virtual destructor?


Incorrect. When you are deleting a derived object D through a base
class pointer B (e.g. the object is of type D, but you are invoking
delete on a (B *), that base class must have a virtual destructor. If
it does not, then it essentially amounts to an attempt to destroy and
delete the object as if it were a B without any D part. The behavior is
undefined then. Not only might the D destructor fail to run, the
operation can cause the program to terminate or behave erratically.
When I am defining such a class with default destructor then my
compiler is giving warning that class XXX has virtual functions but
non-virtual destructor.


The reason the compiler says that is that a class with virtual
functions is almost certainly designed for inheritance, and it's almost
certain that the program will use virtual functions as interfaces to a
derived class through base class references or pointers. Such programs
quite often end up with memory management involving base classes: the
end of an object's lifetime is computed at a point where its exact type
is not know, only a base class.

But that destructor problem could happen without virtual functions. But
the compiler can't warn about those cases without becoming a nuisance.
The compiler would have to gather information about how a class is
used; notice that there is a D derived from B, and that B has no
virtual destructor, and that there are places in the code where a B *
pointer is being deleted. If the compiler could notice all three things
in one compilation pass, it could emit a warning like ``B is being used
for inheritance, pointers to B are being deleted, yet it has a
non-virtual destructor''.


Thanks for the valuable insight on the problem

Jun 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.