virtual method pointer

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Fri May 4 00:48:56 PDT 2012


Thanks, I'll look into it.

On Fri, May 4, 2012 at 11:25 AM, Jacob Carlborg <doob at me.com> wrote:
> On 2012-05-03 20:46, Gor Gyolchanyan wrote:
>>
>> I need to get a pointer to a virtual method, which is in turn a
>> function pointer, being set by virtual method binding.
>> Can anyone, please, tell me how to get it? Taking the delegate of the
>> method won't do, because I need it to behave exactly as a virtual
>> method call, except I pass the "this" explicitly.
>> I need this in an event handling mechanism I'm making. You derive from
>> the Sink class, passing your static type to the constructor, which
>> scans your virtual methods, that conform to specific requirements and
>> extracts them into an array, which later uses to dispatch the incoming
>> events.
>> It will feel much like a run-time virtual template method.
>
>
> You can use two delegates:
>
> class Foo
> {
>    void bar ()
>    {
>        writeln("Foo");
>    }
>
>    void delegate () resolveVirtualCall ()
>    {
>        return &bar;
>    }
>
>    static void forwardVirtualCall (Foo object)
>    {
>        void delegate () delegate () dg;
>        dg.ptr = cast(void*) object;
>        dg.funcptr = &resolveVirtualCall;
>        dg()();
>    }
> }
>
> class Bar : Foo
> {
>    void bar ()
>    {
>        writeln("Bar");
>    }
> }
>
> void main()
> {
>    Foo b = new Bar;
>    Foo.forwardVirtualCall(b);
> }
>
> Will print "Bar". If "resolveVirtualCall" is not used and "bar" is used
> directly instead, it will print "Foo".
>
> --
> /Jacob Carlborg



-- 
Bye,
Gor Gyolchanyan.


More information about the Digitalmars-d mailing list