Why is it necessary for a base class to have a constructor that takes 0 args?

前端 未结 4 1996
迷失自我
迷失自我 2021-02-18 14:18

This won\'t compile:

namespace Constructor0Args
{
    class Base
    {
        public Base(int x)
        {
        }
    }

    class Derived : Base
    {
    }         


        
4条回答
  •  我寻月下人不归
    2021-02-18 14:41

    If you don't explicitly define a constructor for a class, a default constructor is automatically defined, which looks like this:

    public Derived() : base() 
    {
    }
    

    You need to specify the constructor on the base class as well as which arguments to pass to it:

    public Derived() : base(1) 
    {
    }
    

提交回复
热议问题