Pointer to method C++ style

Sergey Gromov snake.scaly at gmail.com
Thu Jul 23 19:09:12 PDT 2009


Thu, 23 Jul 2009 11:54:40 -0400, Steven Schveighoffer wrote:

> On Wed, 22 Jul 2009 23:47:30 -0400, Sergey Gromov <snake.scaly at gmail.com>  
> wrote:
> 
>> Is there a way to declare and statically initialize some sort of pointer
>> to method, and later call it for an actual object instance?
> 
> I don't know why the "non constant expression error" happens, but  
> constructing a delegate from function pointers is pretty simple:

It's my understanding that you cannot construct a delegate from a
function pointer because they use different calling conventions.  Though
you show here that it *is* possible to construct a delegate from another
delegate you dissected earlier.

>          LOOKUP_TABLE[0] = Method("method1", &Component.method1);
>          LOOKUP_TABLE[1] = Method("method2", &Component.method2);

These two lines are weird.  ``pragma(msg)`` shows that type of
``&method1`` is ``void function()`` while it must be ``void delegate()``
for a non-static member because of difference in calling convention.
Actually I think that taking an address of a non-static member in a
static context must be a compile time error.


More information about the Digitalmars-d-learn mailing list