Re: Problem with Method handling a BaseClass parameter
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 21 Apr 2007 07:37:37 +0100
Andreas Schmitt <keldorkatarn@xxxxxx> wrote:
Nicholas Paldino [.NET/C# MVP] schrieb:
You have the example kind of turned around. It really should be this:
My example is the way I want to use it and the way it would work in C++
and I have no idea why it does not work in C#.
I'd be very surprised if it really *did* work in C++. Could you provide
an example which compiles in C++?
This example shows the advantages of polymorphism. With the one you
coded, you had a base class. The compiler can only know about classes that
the base class derives from (in other words, its parents) not about other
classes that derive from that class.
Why must it know? I tell the compiler what function I want not to check
if everything could possibly work under all conditions. I don't care if
the compiler knows all derived classes. I do.
Overloading is performed at compile-time, not execution time. The
compiler needs to know which of the methods to call, and it can't find
any that will definitely work.
The normal way round this is to use double-dispatch. See
http://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_in_C.2B.2B
for some info on this.
Hope this helps.
Sorry but it does not.
I still don't get why C# must be so strict about this. If I cannot use a
Base Class pointer (and those strange C# referenece types are supposed
to be exactly that according to every C# information I read so far) to
transport any object that is an instance of a derived class of the base
then what are Base class pointers for anyway?
You can - you just can't change the fact that overloading is performed
at compile-time.
if I can't get this to work I'll have to overload the handle method for
every single derived class that exists although that method does nothing
but call another method of a different class in which the overloading
becomes necessary. For that one method I'd overload 20 methods which all
do exactly the same. What's the sense of that?
Use polymorphism instead. Put an empty virtual method in the base
class, and override it in each method you actually want to do something
in. Then just call whatever.SomeMethod(myThing).
I begin to really dislike C# and its compiler always telling me what
value can't be converted to whatother and what conversion might not be
safe and whatever. Am I the programmer or the compiler?
(Ignore the outburst, just a frustrated C++ lover talking)
I think you're complaining about something you couldn't do in C++
either though.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- References:
- Problem with Method handling a BaseClass parameter
- From: Andreas Schmitt
- Re: Problem with Method handling a BaseClass parameter
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Problem with Method handling a BaseClass parameter
- From: Andreas Schmitt
- Problem with Method handling a BaseClass parameter
- Prev by Date: Re: threading and delegates
- Next by Date: Recording Audio
- Previous by thread: Re: Problem with Method handling a BaseClass parameter
- Next by thread: How to calulate width of string for given font
- Index(es):
Relevant Pages
|
Loading