472,371 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

Jun 20 '06 #1
3 1938
* 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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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,...
2
by: Chunhui Han | last post by:
Hi, I was recently reading about virtual base classes in C++. The book I was reading says that it is illegal to have non-virtual destructor for the virtual base class. It seems to me that...
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...
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...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
3
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete...
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...
12
by: Avalon1178 | last post by:
Hello, Are default destructors virtual? In other words, say I have "class A" and "class B : public A", and I have the code below: A * a = new A; B * b = new B; a = b;
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.