Saaa wrote:
"Daniel Keep" <daniel.keep.li...@gmail.com> wrote in message news:h88cck$1or...@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;

c --> c2.method
        ^
        |
deleg ---

 deleg = &c.method;
 writefln(deleg()); // 2
 c=c3;

Now:

c --> c3.method

      c2.method
        ^
        |
deleg ---

 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, to previous c's method. See above.

Remember that a delegate is a pair: an instance and it's method. You can't just point to a class' method, you are always pointing to a instance's method.

(that's why my arrows point to the dot :-P)

Reply via email to