My destructor's not getting called

Started by
12 comments, last by Zahlman 19 years, 6 months ago
I have a base class A and a class B that derives from A. I do not define a destructor in A but I do define a virtual destructor in B. I then instantiate an instance of B as follows: A* obj = new B But when I come to delete it: delete obj it doesn't call the destructor I define in B. I'm hoping I don't have to do something as dirt as: delete (B*)obj If the C++ compiler allows for virtual functions, why can't the same be done for destructors?
Advertisement
C++ does support virtual destructors, you just haven't provided one. Create a virtual destructor for A, and all your problems should be solved.

The problem is that, because you don't explicitly define ~A to be virtual, the compiler doesn't allow ~B to overwrite the default ~A [there is a destructor, you just don't write it]. So when you delete a pointer to A, it can't know to call ~B. That probably makes no sense at all, but there you go.

CM
That makes perfect sense. Thanks a lot
Actually, I have another question:

Say you have defined a virtual destructor in class A and B, how can you ensure when deleting a B object, that first class B's destructor gets called, and then A's destructor gets called?
Or is this not possible?
That is automaticly done.
Just write a small test programm to see it yourself.
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
Just tried it and you're right. Thanks again
Quote:Original post by Conner McCloud
C++ does support virtual destructors, you just haven't provided one. Create a virtual destructor for A, and all your problems should be solved.

The problem is that, because you don't explicitly define ~A to be virtual, the compiler doesn't allow ~B to overwrite the default ~A [there is a destructor, you just don't write it]. So when you delete a pointer to A, it can't know to call ~B. That probably makes no sense at all, but there you go.

CM


Just as an open question, why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class? (And in that case, shouldn't the compiler be able to optimize out the virtuality?)
Quote:Original post by Zahlman
Just as an open question, why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class? (And in that case, shouldn't the compiler be able to optimize out the virtuality?)


When you don't want a vtbl pointer in your class (e.g. 3D math vector class). If you don't need polymorphism, you shouldn't have to pay for it. And even though the virtuality may be optimized away when the compiler knows the actual type of the instance (i.e. not a pointer or a reference), the fact that the class does now have a virtual function (and thus needs a vtbl) cannot.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
If I recall correctly, virtual functions require more memory per object instance than non-virtual functions, because the function may reside in a different memory location per instance instead of per object. Therefore, if you really needs the memory, you don't want a virtual destructor.

Hope this helps! [smile]
TuinfeesT!
Quote:Original post by Zahlman
why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class?
When you know that no objects of classes that subclass that class will ever have
delete
called on their base class pointers, and you want to maximize performance for calling
delete
on the derived class pointers. Also, if you have no other virtual functions (probably because you don't intend any subclassing) it'll save you a few bytes and a few constructor instructions for the vtable.

I've only ever seen the former case once, in a trivial bit of code that really didn't need the speed boost. I make a habit of declaring a virtual destructor whenever subclassing will be done.

This topic is closed to new replies.

Advertisement