If I have the following classes:
public abstract class Base
{
public abstract T CreateItem<T>() where T : Base;
}
public class Derived : Base
{
public override T CreateItem<T>()
{
return new Derived(); //Implicit cast error
}
}
Why does the CreateItem method in Derived fail as Derived inherits from
Base and therefore I would have thought the constraints satisfied.
If I try and explicit cast to T it fails with the error "Cannot convert
type Derived to T"
The only way it compiles is if I cast using the as operator.
Cheers
JB 2 1703
The inheritence Derived : Base is unrelated to the "where" clause on a
generic method, since T is supplied by the *caller*, and must return
that T; e.g. I can declare ClassA : Base, and even ClassB : ClassA,
and then call (on a Derived instance) CreateItem<Clas sB>(); CreateItem
then *must* return a ClassB, but isn't going to since Derived and
ClassB cannot be cast.
Perhaps a better option is:
public abstract class Base
{
public abstract T CreateItem<T>() where T : new() { return new
T();}
}
although at the end of the day, the caller could do that themselves!
Perhaps generics aren't the solution... what is the problem you are
trying to solve?
Marc
Marc Gravell wrote:
The inheritence Derived : Base is unrelated to the "where" clause on a
generic method, since T is supplied by the *caller*, and must return
that T; e.g. I can declare ClassA : Base, and even ClassB : ClassA,
and then call (on a Derived instance) CreateItem<Clas sB>(); CreateItem
then *must* return a ClassB, but isn't going to since Derived and
ClassB cannot be cast.
Of course. Doh!
Perhaps a better option is:
public abstract class Base
{
public abstract T CreateItem<T>() where T : new() { return new
T();}
}
although at the end of the day, the caller could do that themselves!
Perhaps generics aren't the solution... what is the problem you are
trying to solve?
Covariant return types :D
I can solve it with generics anyway but I had an abstract class that
creates an instance of another abstract base class and depending on the
concrete implementation it would be different types.
So I either specify the return type as the base and cast or using
generics strong type to the concrete class anyway so all is good.
Cheers,
JB
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jim Newton |
last post by:
hi all, i'm relatively new to python. I find it a
pretty interesting language but also somewhat limiting
compared to lisp. I notice that the language does
provide a few lispy type nicities, but some very
important ones seem to be missing.
E.g., the multiple class inheritance is great,
but there are no generic functions (at least that
i can find). If i have classes X, Y, and Z,
and subclasses X_sub, Y_sub, and Z_sub respectively.
|
by: SimonH |
last post by:
Hi all,
I would like to make a generic set of methods that could be called regardless
of the database behind the scenes.
One of the methods I would like would take a string sql statement and an
array of DataParameter objects.
The problem i have is there doesnt seem to be a generic DataParameter class
that I can instantiate. There only seems to be specific implementations like
|
by: Jethro Guo |
last post by:
C++ template use constraint by signature,It's very flexible to programmer but
complex for complier, and at most time programmer can not
get clear error message from complier if error occur. C# generic use
constraint by type,complier is relaxed, but it is very limited to
programmer.Is there a way to get merits of both?
Maybe the following way can achieve this purpose :
//First add a keyword "constrant" to modify class or struct just...
|
by: Tigger |
last post by:
I have an object which could be compared to a DataTable/List which I am
trying to genericify.
I've spent about a day so far in refactoring and in the process gone
through some hoops and hit some dead ends.
I'm posting this to get some feedback on wether I'm going in the right
direction, and at the same time hopefully save others from going
through the process.
|
by: Andrew Ducker |
last post by:
I have a collection of classes descending from a single root class
(let's call it RootClass). They all currently have a property of
Logical, of type Logical. However they actually return a subclass of
Logical (LogicalA, LogicalB). I'm currently casting them to the right
type when I retrieve them, but obviously this isn't terribly 'nice'.
I tried converting the class to be generic, and having a type parameter
that affected the type of...
| |
by: Steve Richter |
last post by:
in a generic class, can I code the class so that I can call a static
method of the generic class T?
In the ConvertFrom method of the generic TypeConvert class I want to
write, I have a call to the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
|
by: rkausch |
last post by:
Hello everyone,
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two...
|
by: phancey |
last post by:
I'm quite new to generics. I have 2 generic classes:
MyClass<Tand MyOtherClass<T>.
MyClass<Thas 2 public Add methods
Add(MyOtherClass<T>);
Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another class that
contains an object of type T)
I have a third generic class: MyHandler<Twhich contains 2 Add
methods and a generic method:
Add(T obj)
|
by: raylopez99 |
last post by:
Here is a good example that shows generic delegate types. Read this
through and you'll have an excellent understanding of how to use these
types. You might say that the combination of the generic delegate
type expression in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking.
Note that since 'boxing' and 'unboxing' is involved (I think), you
don't get what you...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |