POV-Ray : Newsgroups : povray.off-topic : C# 4.0 Default parameters : Re: C# 4.0 Default parameters Server Time
9 Oct 2024 19:16:15 EDT (-0400)
  Re: C# 4.0 Default parameters  
From: Darren New
Date: 4 Feb 2009 13:40:48
Message: <4989e130@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> 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.
> 
>   Actually that's the most cumbersome solution 

OK. By "better" I meant perhaps "more maintainable" or "more reusable" or 
"more OO".  The way I suggested lets you easily reuse the parser classes in 
other programs, for example. I can write a "-geom" parser and you can write 
a "-regex" parser, and Fred can combine them into one program easily.

>> FWIW, a delegate *is* an object that behaves like a function (your "functor 
>> object").  You write something like
> 
>   Does it also work for member functions?

Yes. Anything that'll take a float and return an integer works. So you can 
combine a static function that takes a float and returns an integer, plus a 
member function that takes an instance (as a distinguised parameter, i.e., 
in "o.xyz()" syntax) and a float and returns an integer, and have them both 
in the delegate at the same time.

>   Member function pointers are a bit complicated in C++, but they can
> be useful (as in my command line parser example). Obviously to call a
> member function of a class through a pointer you need also the object for
> which the function is called. The syntax for calling such a function is:
> 
>     object.*functionPointer(parameters);

Yah. A C# delegate wraps up both the instance and the member method in one 
syntax.  So you'd say

   class Thing {
     static public int pdq(float y) { ... }
     public abc(float z) { return (int) (z*this.stuff); }
     int stuff;
     Thing() { stuff = 28; }
   }

   Thing o = new Thing();
   delegate int xyz(float x);
   xyz = Thing.pdq;
   xyz += o.abc;
   xyz(27.3);  //  I think this will return the result of calling o.abc(27.3)

Pretty handy.

-- 
   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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.