Using arrays of function pointers in D

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 21 09:30:46 PDT 2015


On Thursday, 21 May 2015 at 16:23:15 UTC, John wrote:
> I've been rewriting one of my emulators in D and am fairly new 
> to the language. I'm having trouble finding documentation on 
> creating/initializing/use of arrays of function pointers in D. 
> If anyone has a code example I'd appreciate it!

float foo(int a, float b)
{
     import std.stdio;
     writeln(a, b);
     return a+b;
}

void main()
{
     float function(int, float)[] arrayOfFPs;

     arrayOfFPs = new float function(int, float)[42];

     arrayOfFPs[0] = &foo;

     auto r = arrayOfFPs[0](3, 7.6);
     assert(r == 3+7.6f);

     alias DT = int delegate(int);

     int a = 4;
     auto arrayOfDelegates = new DT[3];
     arrayOfDelegates[1] = (int x) => a + x;
     assert(arrayOfDelegates[1](3) == 7);
}


More information about the Digitalmars-d-learn mailing list