2

Possible Duplicate:
Do I need to explicitly call the base virtual destructor?

Lets say you have the following:

class ParentClass {
    ...
    virtual ~ParentClass();

and

class ChildClass {
    ...
    virtual ~ChildClass();

Which of the destructors would be called? Would both the parent and child's destructors be called? Currently don't have C++ compilers set up on my computer.

Community
  • 1
  • 1
aerain
  • 1,149
  • 3
  • 12
  • 17
  • 1
    Let me introduce you to http://liveworkspace.org/. If you need any other languages or older versions of these compilers, or inputs, http://ideone.com is pretty good for that. – chris Sep 08 '12 at 23:34
  • Funny, I just went back to where I left off in Effective C++ and found this at the top of the page: *The way destructors work is that the most derived class's destructor is called first, then the destructor of each base class is called.* – chris Sep 08 '12 at 23:42

2 Answers2

3

If ChildClass is derived from ParentClass then the derived destructor is called first, followed by the parent class. As it stands in your code, ChildClass does not inherit from ParentClass

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

Yes, both constructors are call: construction and destruction are symmetric: All subobjects get destroyed in exactly the opposite order they were created. For the order of destruction it doesn't matter if the destructor is virtual. The only impact of virtual vs. non-virtual destructors is when deleteing an object of a dreived type using a pointer to a base: This results in undefined behavior if the destructor of the base isn't virtual.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380