How useful would Inheriting Constructors be in C++?

前端 未结 5 664
自闭症患者
自闭症患者 2020-12-08 10:15

As I sit in the C++ Standards committee meetings, they are discussing the pros and cons of dropping Inheriting Constructors since no compiler vendor has implemented it yet (

5条回答
  •  春和景丽
    2020-12-08 10:43

    I see a problem when the new class has member variables that need to be initialized in the constructor. This will be the common case, as usually a derived class will add some sort of state to the base class.

    That is:

    struct B 
    { 
       B(int); 
    }; 
    
    struct D : B 
    { 
       D(int a, int b) : B(a), m(b) {}
       int m;
    }; 
    

    For those trying to solve it: how do you distinguish between :B(a), m(b) and :B(b), m(a) ? How do you handle multiple inheritance? virtual inheritance?

    If only the most simple case is solved, it will have very limited usefulness in practice. No wonder the compiler vendors haven't implemented the proposal yet.

提交回复
热议问题