473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

override virtual, covariant return type

Hi everyone

I'm having huge difficulties overriding or hiding a base function with a
function
that returns a covariant type. I'd be grateful if someone could show me some
code where this is done:
- A class BaseClass is declared with a function me() that takes no arguments
and returns itself, or a pointer or reference to itself.
- A derived class DerivedClass that override me() that takes no arguments
and returns itself, a pointer or reference to itself ("itself" being the
instance of the derived class)
- A piece of code like this: (Warning: this is PSUEDOCODE)
BaseClass foo=new DerivedClass
foo.me() <-this should return a pointer or reference or instance of type
DerivedClass.

I would be VERY VERY gratefull if someone could help me with this
confounding problem, be it in C# or C++.

Cheers
dave
Nov 17 '05 #1
6 2044
OK I'll be more specific. This is what I have now (PSEUDOCODE again):

public class BaseClass
{
public virtual Node me()
{
return this;
}
}

public class DerivedClass: BaseClass
{
new public DerivedClass me()
{
return this;
}
}

static void Main(string[] args)
{
DerivedClass d=new DerivedClass();
BaseClass b=d;
b.me() <<<<<<<<<<<<<<<<<<<<<This should run DerivedClass.Me() and return
the reference to a DerivedClass BUT IT DOESNT. It executes BaseClass.me
INSTEAD.

Please help if you've got any ideas.

Cheers
Dave
Nov 17 '05 #2
Im a newbie, but heres my input.
why don't you try

d.me

since d is the derived class?
-James
"David Sobey" <ds****@NOSPAMugrad.unimelb.edu.au> wrote in message
news:42******@dnews.tpgi.com.au...
OK I'll be more specific. This is what I have now (PSEUDOCODE again):

public class BaseClass
{
public virtual Node me()
{
return this;
}
}

public class DerivedClass: BaseClass
{
new public DerivedClass me()
{
return this;
}
}

static void Main(string[] args)
{
DerivedClass d=new DerivedClass();
BaseClass b=d;
b.me() <<<<<<<<<<<<<<<<<<<<<This should run DerivedClass.Me() and return
the reference to a DerivedClass BUT IT DOESNT. It executes BaseClass.me
INSTEAD.

Please help if you've got any ideas.

Cheers
Dave

Nov 17 '05 #3
To cut a long story short, let's say d doesn't exist any more.

Cheers
dave
Nov 17 '05 #4
"David Sobey" <ds****@NOSPAMugrad.unimelb.edu.au> a écrit dans le message de
news: 42******@dnews.tpgi.com.au...
static void Main(string[] args)
{
DerivedClass d=new DerivedClass();
BaseClass b=d;
b.me() <<<<<<<<<<<<<<<<<<<<<This should run DerivedClass.Me() and return the reference to a DerivedClass BUT IT DOESNT. It executes BaseClass.me
INSTEAD.


First, I would have to ask you why you are using a function to get the
instance that holds the function ?

given :

public class BaseClass
{
public virtual DoIt()
{
Console.WriteLine("Base DoIt");
}
}

public class DerivedClass : BaseClass
{

public override DoIt()
{
Console.WriteLine("Derived DoIt");
}
}

public class Test
{
public void TestMethod()
{
BaseClass bc = new DerivedClass();

// either use

if (typeof(DerivedClass).IsSubclassOf(bc.GetType()))
{
DerivedClass dc = (DerivedClass) bc;
dc.DoIt();
}

// or much more succinctly

if (bc is DerivedClass)
{
DerivedClass dc = (DerivedClass) bc;
dc.DoIt();
}

// or you can safely cast without checking first as long as you
// check the result of the cast for null afterwards

DerivedClass dc = (DerivedClass) bc;
if (dc != null)
dc.DoIt();

// but due to polymorphism, because DoIt() is declared as virtual
// in BaseClass and override in DerivedClass, simply calling DoIt()
// will call the correct version of the method
}
}
////////////////////////////////

Does that solve your problem ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #5
I'm only a very occasional part timer as regards c#, but doesn't the new
keyword in
new public DerivedClass me()
mean that you don't want to override BaseClass.me()
ie I thought it was a versioning thing that permitted you to have a
method with the same name but doesn't override the virtual superclass
method of the same name.
In Java you can't differentiate (ie overload or change the return type
in a subclass) between methods based on return type only wouldn't be
surprised if it's the same in C#.
David Sobey wrote: OK I'll be more specific. This is what I have now (PSEUDOCODE again):

public class BaseClass
{
public virtual Node me()
{
return this;
}
}

public class DerivedClass: BaseClass
{
new public DerivedClass me()
{
return this;
}
}

static void Main(string[] args)
{
DerivedClass d=new DerivedClass();
BaseClass b=d;
b.me() <<<<<<<<<<<<<<<<<<<<<This should run DerivedClass.Me() and return
the reference to a DerivedClass BUT IT DOESNT. It executes BaseClass.me
INSTEAD.

Please help if you've got any ideas.

Cheers
Dave

Nov 17 '05 #6
David Sobey wrote:
OK I'll be more specific. This is what I have now (PSEUDOCODE again):

public class BaseClass
{
public virtual Node me()
{
return this;
}
}

public class DerivedClass: BaseClass
{
new public DerivedClass me()
{
return this;
}
}

static void Main(string[] args)
{
DerivedClass d=new DerivedClass();
BaseClass b=d;
b.me() <<<<<<<<<<<<<<<<<<<<<This should run DerivedClass.Me() and
return the reference to a DerivedClass BUT IT DOESNT. It executes
BaseClass.me INSTEAD.


The new keyword hides the base implementation, it doesn't override it. You
can't change return type when overriding. But you should still be able to
do what you want. Ex:

class ClassA
{
public virtual ClassA me()
{
Console.WriteLine("ClassA.me()");
return this;
}
}

class ClassB : ClassA
{
public override ClassA me()
{
Console.WriteLine("ClassB.me()");
return this;
}
}

ClassB b = new ClassB();
ClassA a = b;
object o = a.me();
Console.WriteLine(o.GetType());
--
Tom Porterfield
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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.
3
by: Matt | last post by:
Hello, I'm trying to implement the design below. I'd prefer to use commented operator+() in class Number instead of the associated, uncommented operator+(), but as I understand Covariant Return...
12
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
15
by: Cliff_Harker | last post by:
Why can't I do this in C# public class A { public A virtual whatever( A a ) { } } public class B : A
5
by: Squeamizh | last post by:
The subject line probably isn't the best, but I don't know how else to describe my question. When I try to compile the code below, my compiler reports an error (error text is below code sample)....
8
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...
3
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...
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
4
by: chambersdon | last post by:
I need to create an abstract class but have the method in the derived class differ by the return type. This is my code: //Abstract class public abstract class DataItem { public abstract...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
jinu1996
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...
0
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.