473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2036
* 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;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.