My destructor's not getting called
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?
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
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
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?
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?
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.
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]
Hope this helps! [smile]
Quote:Original post by ZahlmanWhen you know that no objects of classes that subclass that class will ever have
why would you ever *not* want a destructor to be virtual, except possibly the case of a 'final' class?
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
Popular Topics
Advertisement