How to use a member function (delegate) as a function pointer

Peter Federighi pfederighi at yahoo.com
Sun Oct 31 19:27:06 PDT 2010


Hello.  I'm new to D. It's been a long time since I've coded anything with
classes.  Please excuse my ignorance.

Here's a very simplified version of what I'm trying to do:

import std.c.linux.linux;
import std.stdio;
class FOO
    {
    this()
        {
        sa.sa_handler = &handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART;
        sigaction(SIGALRM, &sa, null);
        }

    ~this()
        {
        sa.sa_handler = SIG_DFL;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART;
        sigaction(SIGALRM, &sa, null);
        }

    private:
    sigaction_t sa;

    void handler(int signal)
        {
        writeln("Got an alarm signal.");
        }
    }


Is there a way to do it without removing handler() from the class?  When I try
compiling, I get: Error: cannot implicitly convert expression (&this.handler)
of type void delegate(int signal) to void C function(int).

Thank you,
- Peter


More information about the Digitalmars-d-learn mailing list