473,503 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Destructors?

Can anyone tell me the reason for this behaviour of virtual
destructors?

#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("\nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("\nDestroying B"); }
};
void main()
{
b *b1=new b;
a *a1=b1;
a1->~a(); //calls Destructor B and A
printf("%d\n",a1->i); // prints 10
delete b1; //calls Destructor A only!!!!
printf("%d\n",a1->i); // prints junk
}

Jul 23 '05 #1
8 1246
am*******@yahoo.com wrote:
| Can anyone tell me the reason for this behaviour of virtual
| destructors?

Your program is ill-formed, and when run, (after changing void main
to int main, so that it will compile, of course) anything can
happen.
--
Robert Bauck Hamar
Jul 23 '05 #2

<am*******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Can anyone tell me the reason for this behaviour of virtual
destructors?

#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("\nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("\nDestroying B"); }
};
You want to change this to "int main()". For more details, see:
http://www.parashift.com/c++-faq-lit....html#faq-29.3
void main()
{
b *b1=new b;
a *a1=b1; a1 and b1 point to the same object.
a1->~a(); //calls Destructor B and A now you destroy the object.

printf("%d\n",a1->i); // prints 10 Here, you invoke undefined behaviour (UB) by trying to play around with an
object that does not exist.
delete b1; //calls Destructor A only!!!! You delete an object twice! Further UB, but now it really doesn't matter
anymore since we're already in the realm of undefined behaviour.
http://www.parashift.com/c++-faq-lit....html#faq-16.2
printf("%d\n",a1->i); // prints junk


More of the same.
UB+anything = UB

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #3

"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3d*************@individual.net...

<am*******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Can anyone tell me the reason for this behaviour of virtual
destructors?

#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("\nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("\nDestroying B"); }
};


You want to change this to "int main()". For more details, see:
http://www.parashift.com/c++-faq-lit....html#faq-29.3
void main()
{
b *b1=new b;
a *a1=b1;

a1 and b1 point to the same object.
a1->~a(); //calls Destructor B and A

now you destroy the object.


Oops!
The destructor is called but the memory is not released.

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #4
That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??

Jul 23 '05 #5
> That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??


What Sumit told you is that you delete an object which no longer exists
(even if the memory was not released). This is evil and results in undefined
behaviour. Your program might also legitimately erase your hard drive and
launch a nuclear missile. The standard authorizes this. Therefore, it is
impossible to explain to you why you specifically get this result as any
result would qualify as undefined behaviour (even working perfectly during
all the development phase and blowing latter in the face of your most
important customer).
--
JS
Jul 23 '05 #6

<am*******@yahoo.com> skrev i en meddelelse
news:11**********************@l41g2000cwc.googlegr oups.com...
That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??

delete has a dual purpose:
1) destroy the object by calling the objects destructor
2) releasing the memory.
You are only allowed to call a destructor once - calling it more times than
one causes undefined behaviour. Also, there is almost never any reason to
call the destructor explicitly as you did. What was the purpose of that?

/Peter
Jul 23 '05 #7

<am*******@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
From the C++ Standard, 12.4/13:
"Once a destructor is invoked for an object, the object no longer exists;
....."

Maybe this will be helpful, too:
http://www.parashift.com/c++-faq-lit....html#faq-11.9
destroyed. Why does "delete b1" then call the destructor of A only??


As mentioned in Jean-Sebastien Samson's posting, you are now attempting to
destroy an object that does not exist.

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #8
am*******@yahoo.com wrote:
That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed.
The object is destroyed. That's what the destructor does. But the memory
isn't released.
Why does "delete b1" then call the destructor of A only??
a1 and b1 are just pointers. By writing "a *a1=b1;", you just made a1 point
to the very same object that b1 points to. So there are _not_ two object,
but still only one. By "a1->~a()", you destroy that object, so now there is
no object left. You still have the memory that the object was in, but C++
doesn't allow you to do much with it. Especially not to try to destroy the
(now non-existing) object again. "delete b1;", when used correctly, does
that. It first destroys the object, then releases the memory it occupied.
Destructor B destroyed successfully in the first attempt??


At that point, there still was an object that could be destroyed.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4537
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...
7
1875
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
23
4434
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
11
2286
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
23
2109
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
4326
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...
37
4121
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
4
1255
by: Zeng | last post by:
I often run into situation where I would like to have a method such as that has derived behavior similar to destructors in c++, is that possible? public BaseClass { private int m_dataInBase;...
5
11333
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
9
2031
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.