473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2040
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.