POV-Ray : Newsgroups : povray.off-topic : C# 4.0 Default parameters : Re: C# 4.0 Default parameters Server Time
6 Sep 2024 13:17:31 EDT (-0400)
  Re: C# 4.0 Default parameters  
From: Darren New
Date: 5 Feb 2009 14:26:06
Message: <498b3d4e$1@news.povray.org>
Warp wrote:
>   But the whole idea with function pointers is that you can call different
> functions (which have the same signature) from the same code.

You can do that. What you can't do (easily) is have a function pointer to an 
instance method that doesn't include the object the instance is invoked on.

In other words, there's no way to declare a function pointer (aka delegate) 
that allows you to declare the "this" parameter outside the normal argument 
list. So you can't say "xyz.my_delegate(27)". You have to invoke 
"my_delegate(xyz, 27)", at which point obviously my_delegate can't point to 
a static function that doesn't have a "this" argument. The terrors of 
distinguished caller syntax.

You can call any function that has the same signature. If you declare
   delegate int xyz(float x);
   xyz fun_ptr;
then any variable of type xyz (such as fun_ptr) can accept a pointer to any 
function that takes a float as an argument and returns an integer. It 
doesn't matter if it's a static function or an instance function, but *if* 
it's an instance function, you have to pass in the instance when you create 
the pointer, because there's no place else to do it - nowhere in the 
declaration of type "xyz" is there a mention of the instance object, so if 
it's an instance function, you have to bind the instance before you assign 
it to the pointer.  I.e., in C++, if you have a pointer to a member 
function, you can't invoke it without providing the member when you invoke 
it. In C#, if you have a pointer to a function, you have to provide the 
member it points to along with the function it points to when you create a 
pointer to a member function.

You cannot conveniently (i.e., without code) declare a pointer (or delegate) 
that points to an instance function without providing the instance. You 
*can* declare a function that takes an instance and arguments and invokes 
the appropriate function, then put that in a delegate and pass in which 
object you want to use when you invoke it. I.e., you can write a one-liner 
that takes "this" from an argument and sticks it before the dot, and then 
assign that one-liner to a delegate.

Me, I'm having a hard time imagining a use-case for a pointer to a member 
function that *doesn't* carry an object reference with it. I.e., I can't 
think of functionality I'd be coding where I know what function I want to 
call when I create the pointer, but I don't know what object I want to call 
it on. Can you offer an example?


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