Inheriting constructors

前端 未结 6 2084
无人及你
无人及你 2020-11-22 09:16

Why does this code:

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
};

int main(void)
{
    B *b = new B(5);
    delete b;
}
         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 09:54

    You have to explicitly define the constructor in B and explicitly call the constructor for the parent.

    B(int x) : A(x) { }
    

    or

    B() : A(5) { }
    

提交回复
热议问题