virtual method pointer
Gor Gyolchanyan
gor.f.gyolchanyan at gmail.com
Thu May 3 12:01:31 PDT 2012
class B: A
{
void foo()
{
writeln("B.foo called");
}
}
void main()
{
auto a = new A();
auto fn = &a.foo;
auto ptr = fn.funcptr;
auto b = new B();
(cast(void function(A))ptr)(b);
}
will this work?
On Thu, May 3, 2012 at 10:54 PM, Alex Rønne Petersen
<xtzgzorex at gmail.com> wrote:
> On 03-05-2012 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.
>>
>
> import std.stdio;
>
> class A
> {
> void foo()
> {
> writeln("foo called");
> }
> }
>
> void main()
> {
> auto a = new A();
> auto fn = &a.foo;
> auto ptr = fn.funcptr;
> (cast(void function(A))ptr)(a);
> }
>
> Prints "foo called".
>
> --
> - Alex
--
Bye,
Gor Gyolchanyan.
More information about the Digitalmars-d
mailing list