Hello everyone,
What is the purpose of virtual destructor? If currently, no derived class?
thanks in advance,
George
Hello everyone,
What is the purpose of virtual destructor? If currently, no derived class?
thanks in advance,
George
To allow for derived classes in the future to have objects that can be safely deleted polymorphically through a pointer to this base class. Or, to put it in another way, to show that this class is designed to be a base class.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Does that mean if you dont define virtual destructor - destructor wont be called in derived class?
A base class's destructor will not be called when the derived class is destructed if it (the base's destructor) is not virtual.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
It will be called, unless you happen to delete it via a base class pointer, in which case it would be, uh, partially destroyed, or some other strange thing like that.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There is always a call to SOME destructor when an object is deleted [even if you don't even have a destructor in your class - in which case a default destructor is defined by the compiler - it may even optimize the then empty destructor out, as it doesn't really do anything].
The difference between a virtual and a non-virtual destructor is the determination of WHICH destructor to call. If you have this code:
If we assume that the destructor is non-virtual, then the One will call the base destructor - because the pointer is of class base. The Two will call the derived destructor.Code:class base { ... }; class derived: public base { ... }; base *bp = new derived; derived *dp = new derived; ... delete *bp; // One ... delete *dp; // Two
If we change the base destructor by adding virtual, then the destructor indicated by the object's vtable will be called, in this case both One and Two will call the derived destructor - which of course is what you want, since derived may need more "destruction" than the base.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If you delete an object of a derived class through a pointer to that derived class, its destructor will certainly be called. This destructor will then invoke the base class destructor, so all is well.
If you delete an object of a derived class through a pointer to its base class, and the base class destructor is not virtual, we could reason that the base class destructor will be called, but not the derived class destructor. The behaviour in such a case is undefined.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Of course, since I already stated that we are dealing with "an object of a derived class through a pointer to its base class". Actually, I think that objects of a derived class are objects of the base class, but the normal polymorphic substitution fails here for deletion if the base class destructor is not virtual.I thought this was well-defined: the base-classes destructor is called. Or did you mean that "calling the base classes destructor when the actual object isn't of that type is undefined".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, I'm just trying to understand what part is "undefined". The derived class will always contain the base-class, so it should be safe to call the base-classes destructor, everything should be there. But as you say, the destruction of anything that is specific to the derived class would not happen, and that's obviously bad.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If you delete a base class pointer that points to a derived class, then the behavior is undefined (and apparently in practice what usually happens is that the derived class destructor is not called).The red code causes undefined behavior.Code:class base { public: ~base() { } }; class derived : public base { }; int main() { base* bp = new Derived; delete bp; }
I'd say, if you really want to know what gets called when, create a base & derived class that print a message in their destructors, then play around with different scenarios (virtual, non-virtual, pointer to base, pointer to derived...).