Using C++ base class constructors?

后端 未结 5 1998
挽巷
挽巷 2020-12-23 13:36

While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations. I w

5条回答
  •  感动是毒
    2020-12-23 13:45

    No, that's not how it is done. Normal way to initialize the base class is in the initialization list :

    class A
    {
    public: 
        A(int val) {}
    };
    
    class B : public A
    {
    public:
      B( int v) : A( v )
      {
      }
    };
    
    
    void main()
    {
        B b(10);
    }
    

提交回复
热议问题