I keep running into the scenario below over and over again. Currently I get
around not having covariant return types by using an interface and explicit
property definitions which works to some extent, but causes other problems
that occur with explicit definitions and also leads to much more code that
is also more difficult to maintain. The new keyword is not even an option,
if used in the example below Offspring on a Dog when in the context of an
Animal would return null even the Dog class has populated the Dog _offstring
Array. The new keyword is also extremely difficult to debug when the above
null scenario is encountered. As a rule I do not use new, and it is my
opinion that the new keyword should never be used in this context.
My Questions are:
Shouldn't covariant return be part of csharp, I can think of no reason they
would ever cause a problem?
Is the class below a poor design and is there a better way to achieve the
same effect without using covariant return types, and preferably without
interfaces?
How do you overcome the lack of covariant return types?
Thanks,
Mike
public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring }
}
private Animal[] _offspring = null;
}
public class Dog : Animal
{
public override Dog[] Offspring
{
get { return this._offspring }
}
private Dog[] _offspring = null;
} 2 1472
I have been researching this again and it looks like covariant return types
*won't* be in cssharp 2.0 so in the meantime I think this is the best
workaround and would like to hear any comments.
In the example below I have chosen to use the new keyword and even though
some boxing is involved as a rule I will always interact with the base
class, preventing the nasty null error that often occurs with new when you
do not rely on the base class to hold the object reference. I will just
have to live with the performance hit for now. I think this is the best
workaround for the lack of covariant return types, eventhough interfaces can
be used, they introduce other issues. I do not like casting of any kind and
see it as an invitation for runtime errors but this one should be fairly
safe until things get better.
I am also going to research moving to managed C++ at some point if csharp
does not start fully supporting polymorphism.
public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring ; }
set { this._offspring = value; }
}
private Animal[] _offspring = null;
}
public class Dog : Animal
{
public new Dog[] Offspring
{
get { return base.Offspring as Dog; }
set { base.Offspring = value; }
}
}
"Mike" <mi********@com .nospam> wrote in message
news:eU******** ******@TK2MSFTN GP12.phx.gbl... I have been researching this again and it looks like covariant return types *won't* be in cssharp 2.0 so in the meantime I think this is the best workaround and would like to hear any comments.
In the example below I have chosen to use the new keyword and even though some boxing is involved as a rule I will always interact with the base class, preventing the nasty null error that often occurs with new when you do not rely on the base class to hold the object reference. I will just have to live with the performance hit for now. I think this is the best workaround for the lack of covariant return types, eventhough interfaces can be used, they introduce other issues. I do not like casting of any kind and see it as an invitation for runtime errors but this one should be fairly safe until things get better.
Ya, new is the best you are going to get here. Generics might help in 2.0,
but otherwise new is what you are going to have to use.
A generic example:
public abstract class Animal<T> where T : Animal<T>
{
public T[] Offspring
{
get {...}
}
}
public class Dog : Animal<Dog>
{
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex Vinokur |
last post by:
Hello,
Here is some program with virtual constructors.
Is there any difference between
* clone1() vs. clone2()
* create1() vs. create2() ?
It seems that it should be.
|
by: Sankar Nemani |
last post by:
Hi,
Does anyone know what the reason behind not allowing to
use "covariant return types when overriding methods in
derived classes" in C# is?
Also while other OO languages such as Java, don't allow
todo the same, why does C++ allow it and then the C# and
VB.NET don't allow?
I just want to understand the logical reasoning behind
this decision if there is one.
|
by: Stefan Slapeta |
last post by:
Hi,
this code does not compile in C#:
class base_class {}
class derived_class : base_class {}
class A
{
public virtual base_class f()
|
by: Stephen Walch |
last post by:
Error C2392 is hitting me hard!
I have a managed C++ library that implements a bunch of fixed interfaces.
For example, one interface is:
public abstract interface IDbCommand
{
public abstract new System.Data.IDbConnection Connection
}
|
by: Bob Hairgrove |
last post by:
Consider the classic clone() function:
class A {
public:
virtual ~A() {}
virtual A* clone() const = 0;
};
class B : public A {
public:
| |
by: miked |
last post by:
Why are there still no covariant return types?
All searches reveal no workarounds accept for using an interface which is a
real pain. I wind up missing this capability almost every time I inherit a
class. The best workaround and one that I use is the new keyword and cast
the base class reference, but in my opinion this is a bad practice leading to
error prone code.
Can someone from MS chime in on this and provide some feedback?
|
by: Alex Vinokur |
last post by:
Here is a code from
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8
--------------------------------------
class Shape {
public:
virtual ~Shape() { } // A virtual destructor
virtual void draw() = 0; // A pure virtual function
virtual void move() = 0;
...
|
by: kikazaru |
last post by:
Is it possible to return covariant types for virtual methods inherited
from a base class using virtual inheritance?
I've constructed an example below, which has the following structure:
Shape = base class
Triangle, Square = classes derived from Shape
Prism = class derived from Shape
TriangularPrism, SquarePrism = classes derived from Triangle and
Prism, or Square and Prism respectively
|
by: Rahul |
last post by:
Hi Everyone,
I was trying to implement covariant return types and i get a
compilation error,
class BB : public AA
{
};
class A
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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.
| | |