Force all classes to implement / override a 'pure virtual' method in multi-level inheritance hierarchy

我是研究僧i 提交于 2019-11-27 09:10:06

I found one mechanism, where at least we are prompted to announce the overridden method explicitly. It's not the perfect way, but at least somewhat near.

Suppose, we have few pure virtual methods in class B (base):

class B {
  virtual void foo () = 0;
  virtual void bar (int) = 0;
};

Among them, I want foo() to be overridden by the whole hierarchy, then abstract foo() 1 level up for simplicity:

class Register_foo {
  template<typename T>  // this matches the signature of 'foo'
  Register_foo (void (T::*)()) {}
};
class Base : public virtual Register_foo {  // <---- virtual inheritance
  virtual void bar (int) = 0;
  Base () : Register_foo(&Base::foo) {}  // <--- explicitly pass the function name
};

Every subsequent child class in the hierarchy would have to register its foo inside its every constructor explicitly. e.g.:

struct D : B {
  D () : Register_foo(&D::foo) {}
  D (const D &other) : Register_foo(&D::foo) {}
  virtual void foo () {};
};

This registration mechanism has nothing to do with the business logic. But at least inside the constructor of any child class it would be mentioned that, which foo it's using.
Though, the child class can choose to register using its own foo or its parent's foo, but at least that is announced explicitly.

James Kanze

What you're basically asking for is to require that the most derived class implement the functiom. And my question is: why? About the only time I can imagine this to be relevant is a function like clone() or another(), which returns a new instance of the same type. And that's what you really want to enforce, that the new instance has the same type; even there, where the function is actually implemented is irrelevant. And you can enforce that:

class Base
{
    virtual Base* doClone() const = 0;
public:
    Base* clone() const
    {
        Base* results = doClone();
        assert( typeid(*results) == typeid(*this) );
        return results;
    }
}

(In practice, I've never found people forgetting to override clone to be a real problem, so I've never bothered with something like the above. It's a generally useful technique, however, anytime you want to enforce post-conditions.)

A pure virtual means that to be instantiated, the pure virtual must be overridden in some descendant of the class that declares the pure virtual function. That can be in the class being instantiated or any intermediate class between the base that declares the pure virtual, and the one being instantiated.

It's still possible, however, to have intermediate classes that derive from one with a pure virtual without overriding that pure virtual. Like the class that declares the pure virtual, those classes can only be used as based classes; you can't create instances of those classes, only of classes that derive from them, in which every pure virtual has been implemented.

As far as requiring that a descendant override a virtual, even if an intermediate class has already done so, the answer is no, C++ doesn't provide anything that's at least intended to do that. It almost seems like you might be able to hack something together using multiple (probably virtual) inheritance so the implementation in the intermediate class would be present but attempting to use it would be ambiguous, but I haven't thought that through enough to be sure how (or if) it would work -- and even if it did, it would only do its trick when trying to call the function in question, not just instantiate an object.

In your example, you have not declared D::foo pure; that is why it does not need to be overridden. If you want to require that it be overridden again, then declare it pure.

If you want to be able to instantiate D, but force any further derived classes to override foo, then you can't. However, you could derive yet another class from D that redeclares it pure, and then classes derived from that must override it again.

Is there any practical way of having this effect in C++?

No, and for good reason. Imagine maintenance in a large project if this were part of the standard. Some base class or intermediate base class needs to add some public interface, an abstract interface. Now, every single child and grandchild thereof would need to changed and recompiled (even if it were as simple as adding using D::foo() as you suggested), you probably see where this is heading, hells kitchen.

If you really want to enforce implementation you can force implementation of some other pure virtual in the child class(s). This can also be done using the CRTP pattern as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!