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

Delegate Within Object Example

Part of DelegateCategory

Description

A simple example of how to use delegates within objects, as delegates are usually used for GUI event handling such as in Delphi or C#.

Example

// Simple sample: how to use delegates within objects

import std.c.stdio;

// DelegateClass: a class with declared delegate function "printText"
class DelegateClass 
{
    public void delegate(char[] ) printText;
}

int main()
{
    // Function print
    void print(char[] text) { printf(text); }

    // Initialize a DelegateClass
    DelegateClass delclass = new DelegateClass();

    // Assign the delegate "printText" to function "print"
    delclass.printText = &print;

    // Call printText to test

    delclass.printText("HELLO\n");

    return 0;
}

Source

Link http://www.dsource.org/tutorials/index.php?show_example=102
Posted by Anonymous
Date/Time Fri Jun 25, 2004 2:49 am