Inheritting constructor?

  • Thread starter Thread starter Pavils Jurjans
  • Start date Start date
P

Pavils Jurjans

Hello,

I am having this code:

**************************************************
using System;

public class Parent
{
int a;
public Parent(int arg)
{
a = arg;
}
}

public class Child : Parent
{
public void Meth(string x)
{
Console.WriteLine("Hello from Child.Meth, id={0}", x);
}
}
**************************************************
If I try to compile this, I get error "No overload for method 'Parent' takes
'0' arguments". If I add the following code to the Child class,

public Child(int arg): base(arg)
{
}

All compiles fine. That is, compiler demands that I define constructore for
the Child class. But, what if I want to default to the Parent's constructor?
Why can't I just skip the definition of Childs constructor, and compiler
would assume that Chils constructor is the same as Parent's? Now I am forced
to add thos dummy lines to all Child classes that don't need their own
constructors, they add/overload just methods.

-- Pavils
 
All compiles fine. That is, compiler demands that I define constructore for
the Child class. But, what if I want to default to the Parent's constructor?
Why can't I just skip the definition of Childs constructor, and compiler
would assume that Chils constructor is the same as Parent's? Now I am forced
to add thos dummy lines to all Child classes that don't need their own
constructors, they add/overload just methods.

See http://www.pobox.com/~skeet/csharp/constructors.html
 
Pavils,

The problem you are experiencing has to do with the fact that you do not
have a default (non paramater) constructor in your base (Parent) class. The
onyl way to initialize it is using the constructor which expects an int. To
solve
this you simply add a default constructor to the Parent class. If you do not
want it to be public you can make it protected (private to anyone else
except
for classes which derives from it, for who it will be public).

public class Parent
{
int a;

protected Parent()
{
}

public Parent(int arg)
{
a = arg;
}
}

Now you can inherit from the Parent class without having to explicitly call
the
constructor with an int.

HTH,

//Andreas
 
Hello,

I am having this code:

**************************************************
using System;

public class Parent
{
int a;
public Parent(int arg)
{
a = arg;
}
}

public class Child : Parent
{
public void Meth(string x)
{
Console.WriteLine("Hello from Child.Meth, id={0}", x);
}
}
**************************************************
If I try to compile this, I get error "No overload for method 'Parent'
takes '0' arguments". If I add the following code to the Child class,

public Child(int arg): base(arg)
{
}

All compiles fine. That is, compiler demands that I define
constructore for the Child class. But, what if I want to default to
the Parent's constructor? Why can't I just skip the definition of
Childs constructor, and compiler would assume that Chils constructor
is the same as Parent's? Now I am forced to add thos dummy lines to
all Child classes that don't need their own constructors, they
add/overload just methods.

-- Pavils

Can anyone give me some good reasons that constructors aren't inherited?
This has pissed me off to no extent in the past and I'm just looking for
some explinations. Maybe I'll see the light.
 
StingK said:
Can anyone give me some good reasons that constructors aren't inherited?
This has pissed me off to no extent in the past and I'm just looking for
some explinations. Maybe I'll see the light.

See http://www.pobox.com/~skeet/csharp/constructors.html

I believe a much better thing would be to make the IDE smarter so that
you could easily add a bunch of constructors. That would still lead to
safe code, but with less hassle.

(It works very well in Eclipse...)
 
See http://www.pobox.com/~skeet/csharp/constructors.html

I believe a much better thing would be to make the IDE smarter so that
you could easily add a bunch of constructors. That would still lead to
safe code, but with less hassle.

(It works very well in Eclipse...)

I just had another read of that section on your site. To tell the truth,
I don't like the reasoning behind it.

In fact; the other day, I did need to extend FileInfo (we'll just leave
out the fact that FileInfo is sealed). Blah. Scratch that... That turned
into another rant of .NET ;)

So let's say I have an excelent class in my data layer. I also have a
business layer that has many "manager" classes with nothing but static
methods. Ideally, those static methods will return "business" objects. I
just don't want to reference the data layer at all in the UI.

Now, in my UI layer, I don't want it to rely on the data layer AT ALL.
So, my only choice is to provide "wrapper" classes in my business layer.

The easiest way to do this for this particular project is to simply
create empty classes that inherit from the corresponding data classes.
However, I can't call the constructor on my business class without
declaring something like this:

MyBusClass(int id) : base(id)

What I'd really like to do ( I admit that I'm a ****in' slouch :) ) is
this:

c = new MyBusClass(id)

and have the compiler be smart enought to know that I'm calling a
constructor that lives in the base class. Of course, if I take the time
to create a constructor for every senario in every class, that will work.
But, I want my compiler to work with ME! Not in reverse. haha.

I just don't know why that is such a big deal.

Thanks for your time,
Chris
 
Besides the reasons cited in
http://www.yoda.arachsys.com/csharp/constructors.html, as mentioned by Jon
Skeet , the only line you need to add "public Child(int arg): base(arg){}";
yields all the base constructor's initialization code functionality, no
matter how complex it is.
So while technically constructors are not inherited in C# (for that matter
even C++), their CODE is always available (inheritable), if wired up by the
deriving class constructor.

Fakher Halim
Software Architect
TPG
 
Back
Top