Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualHodgman

Posted 31 October 2012 - 08:32 PM

Yeah a switch is one way to do it, another is to manually construct your own jump table (which the compiler may do for you with a switch), e.g.
void a( int arg ) {}//called if enumValue == 0
void b( int arg ) {}//called if enumValue == 1
void c( int arg ) {}//called if enumValue == 2
typedef void(Fn)(int);
Fn* table[3] = { &a, &b, &c };
void CallByEnum( int enumValue, int arg )
{
    assert( enumValue >= 0 && enumValue < 3 );
    table[enumValue]( arg );
}

#1Hodgman

Posted 31 October 2012 - 08:31 PM

Yeah a switch is one way to do it, another is to manually construct your own jump table (which the compiler may do for you with a switch), e.g.
void a( int arg ) {}//called if enumValue == 0
void b( int arg ) {}//called if enumValue == 1
void c( int arg ) {}//called if enumValue == 2
typedef void(Fn)(int);
Fn* table[3] = { &a, &b, &c };
void CallByEnum( int enumValue, int arg )
{
assert( enumValue >= 0 && enumValue < 3 );
table[enumValue]( arg );
}

PARTNERS