3

If I have a base class and a derived class, and I delcare the destructor in the parent virtual, but instantiate an object of type subclass, when destroyed it will invoke the parent destructor right(since virtual)? If I also declare a destructor in the derived class, will it call both destructors (base and derived). Thanks in advance :-).

The second part to my question is regarding the first. Why does the base class destructor need to be declared virtual. Don't constrcutors cycle up the hiearchy. They don't share the same name, so where's the need for it? Shouldn't it work the same for destrucotrs, or by default is only one called? Also does through late binding is it able to detect all the classes and object is made of?

EDIT: My question is not just about virtual destructors, but why does it need to be declared virtual, since they should all be called by default.

j0k
  • 22,600
  • 28
  • 79
  • 90
rubixibuc
  • 7,111
  • 18
  • 59
  • 98

3 Answers3

8

Yes, parent destructors will be called automatically.

The destructor should be virtualised so a derived instance can be destroyed properly by code that thinks it has a reference to a base class instance.

In very limited circumstances, it is OK not to virtualise, if you really need to save a few cycles on the vtable lookup.

Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • Is the virtual keyword applied differently to destructors and copy constructors? I ask copy contructors becuase it is part of the same idea. With memeber functions the only different you can have is a return type respective of the class. All parameters and function name must be same. How are these other functions -(destructors copy ctors), represented at runtime? Do they all have the same name, or are they called in sequence? Sry about all the question, just really want to understand it :-/. – rubixibuc Apr 26 '11 at 04:54
  • Basically does declaring a destructor higher in the inheritance hierarchy carry down to all destructors even though the name is different? – rubixibuc Apr 26 '11 at 04:56
  • Yes. Constructor & destructor names aren't like regular method names; the special classname::classname and classname::~classname syntax tells the compiler it's a constructor or destructor, so the inheritance looks for constructor/destructor, NOT a specific classname – Phil Lello Apr 26 '11 at 05:05
4

The need for virtual destructors is because of polymorphism. If you have something like the following:

 class A { ... };

 class B : public A { ... };

 void destroy_class(A* input)
 {
     delete input;
 }

 int main()
 {
     B* class_ptr = new B();
     destroy_class(class_ptr); //you want the right destructor called

     return 0;
 }

While a bit of a contrived example, when you delete the passed-in pointer for the destroy_class() function, you want the correct destructor to get called. If the destructor for class A was not declared virtual, then only the destructor for class A would get called, not the destructor for class B or any other derived type of class A.

Stuff like this is very often a fact-of-life with non-template polymorphic data-structures, etc. where a single deletion function may have to delete pointers of some base-class type that actually points to an object of a derived type.

Jason
  • 31,834
  • 7
  • 59
  • 78
2

rubixibuc,

Yeah the subclasses destructor is invoked first, then it's superclass... then it's superclass, and so on until we get to Object's destructor.

More here: http://www.devx.com/tips/Tip/13059 ... it's worth the read... only a screen-full, but it's an INFORMATIVE screen-full.

corlettk
  • 13,288
  • 7
  • 38
  • 52