 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pravesh Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Question regarding virtual functions/destructors |
|
|
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 |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Question regarding virtual functions/destructors |
|
|
* Pravesh:
| Quote: | 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.
| Quote: | 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? |
|
| Back to top |
|
 |
Kaz Kylheku Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Question regarding virtual functions/destructors |
|
|
Pravesh wrote:
| Quote: | 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.
| Quote: | 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''. |
|
| Back to top |
|
 |
Pravesh Guest
|
Posted: Tue Jun 20, 2006 9:10 am Post subject: Re: Question regarding virtual functions/destructors |
|
|
Kaz Kylheku wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|