Question regarding virtual functions/destructors

P

Pravesh

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
 
A

Alf P. Steinbach

* 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.
 
K

Kaz Kylheku

Pravesh said:
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''.
 
P

Pravesh

Kaz said:
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.


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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top