Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

This won't compile:

namespace Constructor0Args {     class Base     {         public Base(int x)         {         }     }      class Derived : Base     {     }      class Program     {         static void Main(string[] args)         {         }     } } 

Instead, I get the following error:

'Constructor0Args.Base' does not contain a constructor that takes 0 arguments

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

like image 401
mpen Avatar asked Feb 11 '11 06:02

mpen


People also ask

Does a base class need a constructor?

If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class. We have to call constructor from another constructor. It is also known as constructor chaining.

Does not have a constructor that takes the parameters ()?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.

What is the significance of making constructor in a derived class?

The constructor of the derived class receives the entire list of required values as its argument and passes them on to the base constructor in the order in which they are declared in the derived class. A base class constructor is called and executed before executing the statements in the body of the derived class.

Why base class constructors are executed first and then derived?

Because m_id lives in the Base portion of the object, the Base constructor is the only constructor that can initialize that value. Note that it doesn't matter where in the Derived constructor member initializer list the Base constructor is called -- it will always execute first.


2 Answers

It isn't - the problem is that it needs to call some base constructor, in order to initialise the base type, and the default is to call base(). You can tweak that by specifying the specific constructor (and arguments) yourself in the derived-types constructor:

class Derived : Base {     public Derived() : base(123) {} } 

For parameters to base (or alternatively, this) constructors, you can use:

  • parameters to the current constructor
  • literals / constants
  • static method calls (also involving the above)

For example, the following is also valid, using all three bullets above:

class Derived : Base {     public Derived(string s) : base(int.Parse(s, NumberStyles.Any)) {} } 
like image 168
Marc Gravell Avatar answered Oct 14 '22 06:10

Marc Gravell


When you derive a class from another, the base class will be called before the derived classes constructor. When you don't explicitly call a constructor you are essentially writing

class Derived : Base {     public Derived() : base()     {     } } 

Since the Base class doesn't have a 0 argument constructor, this is invalid.

like image 21
Brandon Avatar answered Oct 14 '22 07:10

Brandon