If I've got a base class along with a derived class, and that i delcare the destructor within the parent virtual, but instantiate an item of type subclass, when destroyed it'll invoke parents destructor right(since virtual)? Basically also declare a destructor within the derived class, does it call both destructors (base and derived). Thanks ahead of time :-).

The 2nd part to my real question is concerning the first. How come the bottom class destructor have to be declared virtual. Don't constrcutors cycle in the hiearchy. They do not share exactly the same title, so where's the requirement for this? Should not it work exactly the same for destrucotrs, or automatically is just one known as? Also does through late binding could it be in a position to identify all of the classes and object consists of?

EDIT: My real question is not nearly virtual destructors, why must it be declared virtual, given that they really should be known as automatically.

Yes, parent destructors is going to be known as instantly.

The destructor ought to be virtualised so a derived instance could be destroyed correctly by code that thinks it features a mention of the basics class instance.

In very limited conditions, it's OK to not virtualise, should you really have to save a couple of cycles around the vtable research.

The requirement for virtual destructors is due to polymorphism. For those who have something similar to the next:

 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 little of the contrived example, whenever you remove the passed-in pointer for that destroy_class() function, you would like the right destructor to obtain known as. When the destructor for class A wasn't declared virtual, then just the destructor for class A would get known as, not the destructor for class B or other derived kind of class A.

That is extremely frequently a well known fact-of-existence with non-template polymorphic data-structures, etc. in which a single deletion function might have to remove pointers of some base-class type that really indicates an item of the derived type.


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

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