Force a Derived Class to use the Constructor of the Base Class

后端 未结 2 507
半阙折子戏
半阙折子戏 2021-01-19 07:55

Is there a way to force a derived class to use the constructor of the abstract base class? It must not be a real constructor, I have an open mind about creative solutions.

2条回答
  •  悲&欢浪女
    2021-01-19 08:30

    Use initializer list of the derived class' constructor.

    class Base
    {
        Base(int Member, string Text) { //...
        }
    };
    
    class Derived : public Base
    {
        Derived(int Member, string Text) : Base(Member, Text) {
                                        // ^^^^^^^^^^^^^^^^^^
            // ...
        }
    };
    

提交回复
热议问题