프로그래밍/C&C++

switch 문을 대체하는 function pointer

MastersOfSmallWorld 2019. 1. 1. 19:35

1. sample 코드


#include <stdio.h>

typedef void (*func) ();


void foo1()

{

        printf("foo1\n");

}

void foo2()

{

        printf("foo2\n");

}

void foo3()

{

        printf("foo3\n");

}



void switch_with_fpointer( void (*pt2func)() )

{

        pt2func();

}


int main()

{

        func fpointers[] = { foo1, foo2, foo3 };


        fpointers[1]();


        switch_with_fpointer( &foo3 );

}



2. 자료

http://oopweb.com/CPP/Documents/FunctionPointers/Volume/CCPP/FPT/em_fpt.html