PC Review


Reply
Thread Tools Rate Thread

delegates and virtual functions

 
 
puzzlecracker
Guest
Posts: n/a
 
      21st Oct 2008
Do delegates don't respect polymorphisms -- why?

say in this example:

using System;

public delegate string FirstDelegate (int x);


abstract class Base
{
public abstract string Foo(int );

public void Dispatch()
{
FirstDelegate del=new FirstDelegate (Foo);
del(5);
}
}



class Derived
{
public override string Foo(int x)
{
return " "+x;
}
}


class Runner
{

public static void Main()
{
Derived d=new Derived();
d.Dispatch(); //trys to delegate bases
}
}


The same thing happens when Base is NOT abstract class and Foo is
declared as virtual....

Is there a rule don't mix delegates and runtime polymorphism???

 
Reply With Quote
 
 
 
 
Philipp Brune
Guest
Posts: n/a
 
      21st Oct 2008
Hi,

why should delegates not respect polymorphisms ?
This code, works as expected for me. FirstDelegate
points to the implementation of Foo(..) in Derived.
The only thing that leads to a StackOverflowException is
doing

class DerivedOverflow : Derived
{
public override string Foo(int x)
{
return "-" + base.Dispatch();
}
}

which is also expected behaviour from my understandings.

Perhaps you could explain a little more what you mean.

Philipp



puzzlecracker schrieb:
> Do delegates don't respect polymorphisms -- why?
>
> say in this example:
>
> using System;
>
> public delegate string FirstDelegate (int x);
>
>
> abstract class Base
> {
> public abstract string Foo(int );
>
> public void Dispatch()
> {
> FirstDelegate del=new FirstDelegate (Foo);
> del(5);
> }
> }
>
>
>
> class Derived
> {
> public override string Foo(int x)
> {
> return " "+x;
> }
> }
>
>
> class Runner
> {
>
> public static void Main()
> {
> Derived d=new Derived();
> d.Dispatch(); //trys to delegate bases
> }
> }
>
>
> The same thing happens when Base is NOT abstract class and Foo is
> declared as virtual....
>
> Is there a rule don't mix delegates and runtime polymorphism???
>

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      21st Oct 2008
What same thing? Unfortunately, you haven't pasted the exact code [it
doesn't compile...], so we can't know what you are seeing, since any
repairs we make may change the problem...

However, making the most obvious fixes (adding a base class, naming
the argument, etc) - it all works fine and the override in Derived is
called.

Delegates respect polymorphism.

Perhaps tell us what you are seeing?

Marc
[C# MVP]
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      21st Oct 2008
On Oct 21, 10:06*am, Philipp Brune <her...@gmx.de> wrote:
> Hi,
>
> why should delegates not respect polymorphisms ?
> This code, works as expected for me. FirstDelegate
> points to the implementation of Foo(..) in Derived.
> The only thing that leads to a StackOverflowException is
> doing
>
> * class DerivedOverflow : Derived
> * * *{
> * * * * *public override string Foo(int x)
> * * * * *{
> * * * * * * *return "-" + base.Dispatch();
> * * * * *}
> * * *}
>
> which is also expected behaviour from my understandings.
>
> Perhaps you could explain a little more what you mean.
>
> Philipp
>
> puzzlecracker schrieb:
>
> > Do delegates don't respect polymorphisms -- why?

>
> > say in this example:

>
> > using System;

>
> > public delegate string FirstDelegate (int x);

>
> > abstract class Base
> > {
> > *public abstract string *Foo(int );

>
> > * * * public void Dispatch()
> > * * * {
> > * * * * * * FirstDelegate *del=new FirstDelegate (Foo);
> > * * * * * * del(5);
> > * * }
> > }

>
> > class Derived
> > {
> > * * *public override string *Foo(int x)
> > * * * {
> > * * * * * *return " "+x;
> > * * *}
> > }

>
> > class Runner
> > {

>
> > * * * * public static void Main()
> > * * * * {
> > * * * * * * * Derived d=new Derived();
> > * * * * * * * d.Dispatch(); //trys to delegate bases
> > * * * * }
> > }

>
> > The same thing happens when Base is NOT abstract class and Foo is
> > declared as virtual....

>
> > Is there a rule don't mix delegates and runtime polymorphism???


Great! let me get the debug, i thing my telling compiler to think
Base is Base.....
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using delegates with functions =?Utf-8?B?LnBhdWwu?= Microsoft Dot NET 2 4th Jun 2007 05:23 AM
pure virtual functions and /clr Peteroid Microsoft VC .NET 2 13th Nov 2005 03:39 PM
new virtual functions headware Microsoft C# .NET 2 20th Dec 2004 05:51 PM
new virtual functions headware Microsoft C# .NET 0 16th Dec 2004 08:06 AM
CallBack Functions / Delegates Fabiano Maciel Microsoft VB .NET 2 5th Oct 2003 10:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 AM.