Pointer to method C++ style
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jul 24 06:07:30 PDT 2009
On Thu, 23 Jul 2009 22:09:12 -0400, Sergey Gromov <snake.scaly at gmail.com>
wrote:
> 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.
It's because I'm taking the address of the function on the type, not on an
instance. It's not a delegate because there's no "this" pointer yet.
It makes sense to me anyways. A delegate is a normal function pointer
coupled with a hidden context parameter.
I think it should be possible to construct the table statically, since the
functions exist statically (without 'this' pointers). Either I can't find
the right syntax, or it is a bug.
-Steve
More information about the Digitalmars-d-learn
mailing list