C++ is there a way to force a set of methods to be overriden, if one of them have an override in child class?

It a design question. So, I got an abstract class with a lot of pure virtual methods. Sometimes, I realized I don't need to override these methods, because I'm not interested in these functionnalities. So I changed from pure virtual (=0) to a simple overridable empty method. But now, a child class can override one method but not an other related to it. And it may cause problems... Is there a nice way to force the compiler to say, "if you override this method, you should override this one too !" ?

Upvotes: 0

Views: 540

Answers (2)

einpoklum
einpoklum

Reputation: 131986

Use mix-in classes.

You could use mix-in classes to adopt implementations of sets of related methods. Read about mix-ins here (even though it's a Python question):

What is a mixin, and why are they useful?

You use mix-ins when...

  • You want to provide a lot of optional features for a class.
  • You want to use one particular feature in a lot of different classes.

which is your case exactly. So, perhaps something like:

class Person { /* ... */ } ;

template <transportation_t MeansOfTransport>
class GetsAroundBy: Person {  /* ... */ };

template <domicile_kind_t DomicileKind>
class LivesIn : Person {  /* ... */ };


class Urbanite : Person, LivesIn<ApartmentBuilding>, GetsAroundBy<Metro> {  /* ... */ };
class SteppePerson : Person, LivesIn<Yurt>, GetsAroundBy<Horse> {  /* ... */ };

Upvotes: 1

freakish
freakish

Reputation: 56527

A simple solution is to keep the abstract class and to have a default child class, like this:

class AbstractMap<TKey, TValue> {
public:
    virtual TValue Get(TKey&& key) = 0;
    virtual bool TryGet(TKey&& key, TValue& result) = 0;
};


class PartiallyImplementedMap<TKey, TValue> : public AbstractMap<TKey, TValue> {
public:
    TValue Get(TKey&& key) override {
        TValue result;
        if (TryGet(std::forward<TKey>(key), result)) {
            return result;
        }
        throw KeyNotFoundException();
    };
};

Now you can inherit from PartiallyImplementedMap and only implement TryGet if the default implementation satisfies you. Otherwise you can inherit from the AbstractMap and implement the entire thing.

Upvotes: 2

Related Questions