possible generic inheritance bug

Y

yaniv.golan

Hi,

Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.

In the following code, class c2 inherits from c1_generic. c1_generic
has T as a generic paramter, and in the inheritance, class c2 specifies
c3_T as the value of this parameter.

class c1_generic defines 2 constructors, 1 of them taking 1 parameter.

Attempting to instatiate an object of class c2 using this constructor
fails unless c2 also redefines this constructor.

Here is the code to reproduce the problem:

// base generic class. defines 2 constructors, one of them taking 1
parameter
public class c1_generic<T> {
public c1_generic() {
}

public c1_generic(object o) {
}
}

// inherit from c1_generic, with c3_T as the generic class parameter
public class c2 : c1_generic<c3_T> {

// if you remove this constructor, it won't work - complains that
// it doesn't find a constructor taking 1 parameter, even though such
a
// constructor is defined in the parent class
public c2(object o) : base(o) {
}
}

public class c3_T {
}

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
// if you remove the constructor in c2, this
fails - it can't find
// the constructor defined in c1_generic:
c2 o = new c2(this);
}

Comments?

Thanks,

YanivG
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Hi,

Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.

In the following code, class c2 inherits from c1_generic. c1_generic
has T as a generic paramter, and in the inheritance, class c2 specifies
c3_T as the value of this parameter.

class c1_generic defines 2 constructors, 1 of them taking 1 parameter.

Attempting to instatiate an object of class c2 using this constructor
fails unless c2 also redefines this constructor.

Constructors are never inherited, and that holds for non-generic types
as well.

Example:

public class c1
{
public c1()
{
}

public c1(Int32 o)
{
}
}

public class c2 : c1
{
// nothing here
}

static Main()
{
c2 c = new c2(10); // this won't compile
}

If your class has no constructor defined at all, a public one with no
parameters will be defined for you. This will in turn call the
constructor in the base class with no arguments as well, which is why I
defined public c1() in c1, otherwise c2 would not compile either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top