Derived classes and virtual

If I have a base class, derivedOne class, and a derivedTwo class, where the derivedOne class is derived from the base class and derivedTwo class is derived from derivedOne class, does the virtual destructor stay in derivedTwo class (I.e. do I need to explicitly declare virtual on the derivedOne class even if derivedOne class have a virtual function that was never declared in the base class?

If I have a derived class, which is derived from two different classes, one of which has a virtual destructor and the other doesn’t. Does the derived class always have a virtual destructor?

If a class is never used as a base class, but has a virtual function, are there any negatives to that or should the virtual declaration be removed? If there are penalties what are they?
The keyword virtual determines that the function pointer will be listed in the virtual function table. See:

http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/

The reason for a virtual destructor is that all destructors of the derived class are called. Without the destructor being virtual and if you have a pointer to the base class the compiler doesn't know anything about the derived class and does only call the destructor of the base class. See:

http://www.studytonight.com/cpp/virtual-destructors.php

If I have a derived class, which is derived from two different classes, one of which has a virtual destructor and the other doesn’t. Does the derived class always have a virtual destructor?
If a base class has a virtal function all functions in the derived class(es) with the same signature are automatiacally virtual too. So: Yes.

If a class is never used as a base class, but has a virtual function, are there any negatives to that or should the virtual declaration be removed? If there are penalties what are they?
The 'penalty' would be the virtual table [if it is not optimized and thus removed].
Topic archived. No new replies allowed.