Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Delegate of a Function that Returns an Int

Part of DelegateCategory

Description

More complicated than DelegateNonStaticNestedFunctionExample.

Example

struct Foo
{   
    int a = 7;
    int bar() { return a; }
}


int foo(int delegate() dg)
{
    return dg() + 1;
}

void main()
{
    int x = 27;
    int abc() { return x; }
    Foo f;
    int i;

    i = foo(&abc);      /* i is set to 28 */
    i = foo(&f.bar);    /* i is set to 8 */
}

Source

From http://www.digitalmars.com/d/function.html#closures