C++ Question

Had another interview today, and got this question: If you want a virtual base class with only one function, what should that function be? I.e., what is the one and only one function you would define for the class, that you would then declare as virtual?

Destructor?

Sorry readers, but I’m frazzled from the interview.

I of course meant to ask: If you want an ABSTRACT base class with only one function, what should that function be? I.e., what is the one and only one function you would define for the class, that you would then declare as a PURE virtual?

What counts as a function here?

Whether you declare them or not, every class gets a constructor, a destructor, a copy constructor and an assignment operator. So you’re already over the limit if it just means any functions at all.

Other than that, with no context, I don’t think there’s a general answer to this.

I’d vote for constructor, myself.

You can’t have a virtual constructor, let alone a pure virtual one.

Another vote for destructor. I’m probably oversimplifying things here, but if you intend your class to be inherited from (which is always the case for an ABC), it needs a virtual destructor. This is to ensure that cleanup code is run for the actual class, rather than whatever’s pointing to it.

For more on the topic, see http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

Destructor is correct. The usual rule is that any class that has any virtual functions at all must have a virtual destructor. Hence, if you’re only going to get one, that would have to be it.

Note that nothing in the language actually requires this: it’s perfectly possible to have a class full of virtual functions, no virtual destructor, and it works hunky-dory-perfectly – this will be fine as long as the class is never destroyed polymorphically (as Mbossa said).

However, many programmers wouldn’t recognize the distinction, it’s easy to overlook a polymorphic destruction, and in any case it doesn’t meet the desirable criteria of making a class hard to accidentally misuse. Hence the “rule” that classes with virtual methods should have a virtual destructor.

Oops, I missed the clarification that it was an abstract class and a pure virtual method. My answer stands, though, with two caveats:

1 - pure virtual destructors are a little weird; since destructors aren’t overridden in the standard sense, the “pure virtual” meaning of “force subclasses to override this method” doesn’t apply. This is just a trick to force the class to be abstract: classes in C++ are abstract if and only if they declare a pure virtual method. If there’s no other method around to make pure virtual, the destructor can be used as a convenient placeholder. (Bonus weirdness: because destructors aren’t really overridden you have to actually DEFINE the pure virtual destructor, too – many people don’t even know that it’s possible to define a pure virtual function, here it’s mandatory.)

2 - It’s a pretty nitpicky question; I wouldn’t expect my developers to know it, and I’m fairly strict on wanting folks to know details about C++. The usual reason for HAVING an abstract class is to force implementation of some virtual method at a lower point in the class hierarchy. “Effective C++” has a lists a couple instances where it might be useful to have an “empty” abstract class, but it’s still pretty rare.