Inheriting constructors

前端 未结 6 2079
无人及你
无人及你 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:51

    Correct Code is

    class A
    {
        public: 
          explicit A(int x) {}
    };
    
    class B: public A
    {
          public:
    
         B(int a):A(a){
              }
    };
    
    main()
    {
        B *b = new B(5);
         delete b;
    }
    

    Error is b/c Class B has not parameter constructor and second it should have base class initializer to call the constructor of Base Class parameter constructor

提交回复
热议问题