delegate reference
Saaa
empty at needmail.com
Wed Sep 9 08:55:14 PDT 2009
"Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message
news:h88i4n$235l$1 at digitalmars.com...
>
>
> Saaa wrote:
>> "Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message
>> news:h88cck$1orc$1 at digitalmars.com...
>>>
>>> Saaa wrote:
>>>> abstract class C
>>>> {
>>>> int method();
>>>> }
>>>> class C2:C
>>>> {
>>>> int method() return 2;
>>>> }
>>>> class C3:C
>>>> {
>>>> int method() return 3;
>>>> }
>>>> int delegate() deleg;
>>>> void main()
>>>> {
>>>> C c;
>>>> C2 c2 = new C2;
>>>> C3 c3 = new C3;
>>>> c=c2;
>>>> deleg = &c.method;
>>>> writefln(deleg()); // 2
>>>> c=c3;
>>>> writefln(deleg()); // 2
>>>> // I expected this to write 3, why is this not so?
>>> Because you didn't reassign deleg.
>> but isn't deleg pointing to c's method?
>
> Yes... just because you change what c points to doesn't magically
> rewrite all other references.
deleg points to c and c points to c2 and later to c3 while deleg still point
to c.
Thus when calling deleg it points to c which now points to c3, thus writing
'3'.
maybe I don't understand what "C c;" and "c=c3;" does?
Isn't c a pointer to a C object?
>
> You might want to read http://digitalmars.com/d/1.0/type.html#delegates
I've read that one a few times now :D
More information about the Digitalmars-d-learn
mailing list