Re: new virtual functions
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 12/16/04
- Next message: richlm: "Re: Debug and Release code have different behavior"
- Previous message: Jon Skeet [C# MVP]: "Re: Customer Exception class?"
- In reply to: headware: "new virtual functions"
- Next in thread: headware: "Re: new virtual functions"
- Reply: headware: "Re: new virtual functions"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 16 Dec 2004 08:35:02 -0000
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: richlm: "Re: Debug and Release code have different behavior"
- Previous message: Jon Skeet [C# MVP]: "Re: Customer Exception class?"
- In reply to: headware: "new virtual functions"
- Next in thread: headware: "Re: new virtual functions"
- Reply: headware: "Re: new virtual functions"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|