Shorthand to specify that a derived class implements all abstract methods?

Viewed 39

I find that in some of my code which relies heavily on C++'s polymorphism features, I have a lot of repetitive method declarations.

struct Base
{
    virtual void Foo () = 0;
    virtual void Bar () = 0;
};
struct DerivedA : public Base
{
    void Foo () override;
    void Bar () override;
};
struct DerivedB : public Base 
{
    void Foo () override;
    void Bar () override;
};

With a greater number of derived classes like DerivedA and DerivedB, and/or more numerous abstract methods than just Foo and Bar, writing the function signatures in each derived class gets really redundant, and to cut down on entirely repeated code I've started using some macros for convenience but this is probably not a great habit.

My question: Is there any keyword or other syntax feature that I can use in each derived class, that essentially informs the compiler "all abstract methods are overridden for this", instead of typing out all of those methods' signatures?

Concept:

struct DerivedA : public Base 
{
    concrete;
    // ^ Compiler now allows me to instantiate DerivedA and the linker expects
    // DerivedA::Foo () and DerivedA::Bar() implementations to exist, just as if
    // I had listed every pure virtual function as being overridden explicitly.
};

If this does not exist in C++, out of curiosity are there other languages that have this kind of mechanism, and are there any obvious pitfalls w/ this idea?

0 Answers
Related