|
|
Warp wrote:
> Mike Raiford <"m[raiford]!at"@gmail.com> wrote:
>> At least in C# you can't do function pointers.
>
> There are situations where function pointers are actually useful.
To be clear, C#'s "delegates" are actually a superset of the functionality
of "function pointers". C# wouldn't have any problem with implementing your
examples.
> The next best thing would be to have just one generic virtual function
> in the base class which gets called when a registered command-line parameter
> appears, and then you implement this virtual function. However, you would
> have to implement it as a big if... else if... else if... else if... else
> block, so it becomes more cumbersome than the way described above.
I think better would be the way you wind up having to do it in Java - you
have a class that implements a "parse this" method for each possible
argument to be parsed, and the parser has a map from "-debug" to an instance
of the class that parses the debug parameter. So you have one subclass for
each parser function. Ugly, but not as bad as a giant switch statement.
FWIW, a delegate *is* an object that behaves like a function (your "functor
object"). You write something like
delegate int xyz(float);
int doodoo(float x) { return (int) (x*x); }
xyz = doodoo;
int answer = xyz(27.3);
You can also then say
int foofoo(float y) { return (int) (y * 18.2); }
xyz += foofoo;
Then when you call
xyz(71.3);
it passes 71.3 to doodoo then to foofoo.
Less helpful if you actually need the results, but often delegates are used
as things like callbacks for user interface frobbings or other async events,
so failures tend to be propagated back as exceptions rather than return results.
--
Darren New, San Diego CA, USA (PST)
"Ouch ouch ouch!"
"What's wrong? Noodles too hot?"
"No, I have Chopstick Tunnel Syndrome."
Post a reply to this message
|
|