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 2 1619 ya*********@gmail.com wrote: 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.
--
Lasse Vågsæther Karlsen http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jasper Kent |
last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to
avoid long instantiation names.
I can do this okay:
using BigDictionary = System.Collections.Generic.Dictionary<int,...
|
by: Adam Clauss |
last post by:
OK, I have class A defined as follows:
class A
{
A(Queue<B> queue) { ... }
}
Now, I then have a subclass of both classes A and B. The subclass of A
(SubA), more specifically is passed a...
|
by: Lars |
last post by:
Hi,
I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?
I am fairly new to generics, but I am...
|
by: steve yee |
last post by:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.
|
by: AdawayNoSpam |
last post by:
Said that I have the following class
Class MyRootClass(Of T)
End Class
Class MySubClass1(Of T)
Inherits MyRootClass(Of T)
End Class
|
by: abiz |
last post by:
What is the current thinking about specializing generic C# types?
I've come across a situation where I'd like to do something like this:
struct X<T,U>
{
...
}
struct XCommonCase<T=...
|
by: Angel Mateos |
last post by:
I have this structure:
Class ElemBase
Class Elem1 : Inherits ElemBase
Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits
System.ComponentModel.BindingList(Of GenElem)
Class Colec1...
|
by: =?Utf-8?B?QkogU2FmZGll?= |
last post by:
We have a class that has a public property that is of type List<T>. FXCop
generates a DoNotExposeGenericLists error, indicating
"System.Collections.Generic.List<Tis a generic collection designed...
|
by: SimonDotException |
last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |