views:

363

answers:

9

I have an abstract class with a couple pure virtual functions, and one of the classes I derive from it does not use one of the pure virtual functions:

class derivative: public base
{
public:
    int somevariable;
    void somefunction();
};

anyways, when I try to compile it, I get an error (apparently a class is still considered abstract is derive it from an abstract class and don't overload all pure virtual functions). Anyways, it seems pointless to define a function

int purevirtfunc(){return 0;}

just because it needs to be defined through a technicality. Is there anyway to derive a class from an abstract class and not use one of the abstract class's pure virtual functions?

+1  A: 

When you inherit from a class that has pure virtual functions, you MUST implement those functions. If you don't, then your derived class is also abstract, and you can't create an object of the derived class.

Tim Rupe
You can also create a null implementation that you inherit from that does the stubbing out if you inherit from this interface frequently.
jeffamaphone
+9  A: 

The purpose of deriving from abstract classes is that external code can use the abstract class and expect that all functions have been implemented properly. Being able to unimplement a method would defeat this purpose, making the code uncompilable. You're free to throw an exception, if you so choose, however.

Blixt
+30  A: 

If your derived class doesn't "use" the base class pure virtual function, then either the derived class should not be derived from the base, or the PVF should not be there. In either case, your design is at fault and needs to be re-thought.

And no, there is no way of deleting a PVF.

anon
+1  A: 

No. Either provide a default implementation in the base class or a simple implementation in the derived class, as you suggested.

Peter Ruderman
+11  A: 

A pure virtual class is an interface, one which your code will expect to be fulfilled. What would happen if you implemented that interface and didn't implement one of the methods? How would the code calling your interface know that you didn't implement the method?

Your options are:

  1. Implement the method as you describe (making it private would indicate that it shouldn't be used).
  2. Change your class hierarchy to take into consideration the design change.
jonnii
+5  A: 

It's not a technicality at all. If your derived class does not exhibit all of the behavior of the parent, it should not be derived from the parent. This is a major design smell, and you probably need some design refactoring.

Robert
A: 

Allowing this wouldn't make any sense. What would happen if you called the function without an implementation? A runtime error (that would be silly)? You could argue that it could a compile time error in some cases, but this is not possible if the exact type is not known (e.i. you pass a pointer to an instance of the derived class to a function).

Zifre
+1  A: 

There were already good answers, but if you want more info from the theoretical OO design side, check out the Liskov substitution principle.

abababa22
A: 

As many people have already stated, it sounds like either the base method shouldn't be pure virtual or you should rethink whether your derived class really ISA base.

However, it is possible to provide an implementation for the pure virtual method in the base class. This can act like a default implementation for derived classes, but you still require the derived class to choose the base class's implementation explicity.

I don't know if that will help you with your problem or not.

Matt Ryan