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

Delegate Within Object Example

Part of DelegateCategory


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#.


// 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


    return 0;


Posted by Anonymous
Date/Time Fri Jun 25, 2004 2:49 am