Inheriting constructors

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

    If your compiler supports C++11 standard, there is a constructor inheritance using using (pun intended). For more see Wikipedia C++11 article. You write:

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

    This is all or nothing - you cannot inherit only some constructors, if you write this, you inherit all of them. To inherit only selected ones you need to write the individual constructors manually and call the base constructor as needed from them.

    Historically constructors could not be inherited in the C++03 standard. You needed to inherit them manually one by one by calling base implementation on your own.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 09:52

    This is straight from Bjarne Stroustrup's page:

    If you so choose, you can still shoot yourself in the foot by inheriting constructors in a derived class in which you define new member variables needing initialization:

    struct B1 {
        B1(int) { }
    };
    
    struct D1 : B1 {
        using B1::B1; // implicitly declares D1(int)
        int x;
    };
    
    void test()
    {
        D1 d(6);    // Oops: d.x is not initialized
        D1 e;       // error: D1 has no default constructor
    }
    
    0 讨论(0)
  • 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) { }
    
    0 讨论(0)
  • 2020-11-22 09:55

    How about using a template function to bind all constructors?

    template <class... T> Derived(T... t) : Base(t...) {}
    
    0 讨论(0)
  • 2020-11-22 09:58

    Constructors are not inherited. They are called implicitly or explicitly by the child constructor.

    The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.

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

    UPDATE: In C++11, constructors can be inherited. See Suma's answer for details.

    0 讨论(0)
提交回复
热议问题