Re: new virtual functions
From: headware (dkland_at_cox.net)
Date: 12/20/04
- Next message: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Previous message: Roger Webb: "Re: Reflection Values"
- In reply to: Jon Skeet [C# MVP]: "Re: new virtual functions"
- Messages sorted by: [ date ] [ thread ]
Date: 20 Dec 2004 09:51:00 -0800
I see. You're right, that doesn't seem like something you would use too
often, but that does solve the problem you mentioned. Thanks for the
reply.
Dave
Jon wrote:
> headware <dkland@cox.net> wrote:
>
> <snip>
>
> > It makes sense to me that cRef.f() prints out "D.f()", that is
basic
> > polymorphism. What doesn't make that much sense to me is why
aRef.f()
> > prints out "B.f()". Why should a reference of type A pointing to an
> > object of type D call B.f()? I realize that it has to do with the
fact
> > that f() is declared as new virtual in C. What I don't understand
is
> > why this is the correct behavior. Why would you even want to do
> > something like this? How could this be used?
>
> B.f() overrides A.f(), which is basically what you're calling when
you
> use aRef.f(). C.f() and D.f() are completely separate methods to
A.f()
> and B.f() as far as the compiler is concerned.
>
> As for why/when you'd want to use it - very rarely, basically. The
> usual situation would be if you've got a library class and a class
> you've derived from it. You've put in a method called Foo(), and then
> in the next version of the library that class gains a method called
> Foo() as well, which might be meant to do something completely
> different. You don't want to override the library class's Foo()
method,
> because then the library class would be expecting it to do one thing
> and it would actually do another, but you still want your code which
> uses your Foo() method to compile and run properly.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
- Next message: Richard Blewett [DevelopMentor]: "Re: Is this thread safe?"
- Previous message: Roger Webb: "Re: Reflection Values"
- In reply to: Jon Skeet [C# MVP]: "Re: new virtual functions"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|