override virtual, covariant return type

  • Thread starter Thread starter James Gockel
  • Start date Start date
J

James Gockel

Im a newbie, but heres my input.
why don't you try

d.me

since d is the derived class?
-James
 
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
 
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 said:
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
 
David said:
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());
 
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
 
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
 
Back
Top