October 26th, 2006, 1:22 pm
QuoteOriginally posted by: greenmaxQuoteOriginally posted by: AthleticoMy answer to the original q would be that some classes should not be used as a base class, and declaring your destructor virtual is inviting others to inherit from it. I think Java lets you express such design intentions with a 'final' keyword or something like that. In C++ it's a little more subtle b/c there's no final keyword -- you simply don't use virtual functions.How does that help ? The way I see it is that Java prevents you from inheriting from the class by using a Final keyword. But in c++ you can inherit from a class without a virtual destructor. It might be bad programming practice to do so, but the compiler lets you do so. So in the context of the original, it might be a "good" thing to have a virtual destructor so that the careless programmer does not ignore the destructor of the derived class, when using pointer of the same type as base class ??I understand what you're saying; it's difficult to know what sort of answer the interviewer was looking for. Was C++ object-oriented design philosophy addressed in the interview? Or was it more about nitty-gritty systems programming questions & runtime issues (memory consumption)? 'Dangerous' is a loaded word, but to me it just means 'don't do it or else' (i.e. bad programming practice as you say). For what it's worth, if it were me interviewing I would have accepted your size answer with full credit.Either derived classes should be able to delete their objects polymorphically (thru a pointer to your base class), or they shouldn't. If polymorphic deletion is allowed, then your destructor must be virtual (and public). Otherwise, it should be non-public and non-virtual to get the compiler to help you prevent bugs. Policy classes are a good example of a base class that is not intended to be used polymorphically - their destructors should be protected and non-virtual.