Page 1 of 1

## Jump Tables In Classes Rate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=223327&amp;s=d343430983ad1140e2578f35cd456973&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Martyn.Rae Reputation: 556
• Posts: 1,436
• Joined: 22-August 09

Posted 20 March 2011 - 10:36 PM

Jump Tables In Classes

Introduction

There are times when it is more beneficial to use jump tables in C++ code, rather than a switch statement. This tutorial walks you through the differences between these two approaches.

Using the Switch Statement

The code below, gets a number in the range 0 to 4 and, using a switch statement directs the flow to one of five routines. The routine simply announces itself and returns. When the user enters the digit 9, the run terminates.

```#include <iostream>

using namespace std;

class A {
private:
void A0(void) { cout << "This is A0" << endl; };
void A1(void) { cout << "This is A1" << endl; };
void A2(void) { cout << "This is A2" << endl; };
void A3(void) { cout << "This is A3" << endl; };
void A4(void) { cout << "This is A4" << endl; };
public:
A() {  };
void run() {
int jumper;
while ( true ) {
cout << "Please enter a digit in the range 0-4. "
"Enter 9 to terminate.";
cout << endl;
cin >> jumper;
switch ( jumper ) {
case 0: A0();
break;
case 1: A1();
break;
case 2: A2();
break;
case 3: A3();
break;
case 4: A4();
break;
case 9: return;
default:
cout << "Invalid number" << endl;
}
}
cout << endl;
}
};

int main(int argc, char**argv) {
A this_object;

this_object.run();
return 0;
}

```

Using a Jump Table

The code below uses a jump table in order to direct the flow to the correct routine.

```#include <iostream>

using namespace std;

typedef void (*jump)(void);
class A {
private:
static jump jump_table;
static void A0(void) { cout << "This is A0" << endl; };
static void A1(void) { cout << "This is A1" << endl; };
static void A2(void) { cout << "This is A2" << endl; };
static void A3(void) { cout << "This is A3" << endl; };
static void A4(void) { cout << "This is A4" << endl; };
public:
A() {  };
void run() {
int jumper;
while ( true ) {
cout << "Please enter a digit in the range 0-4. Enter 9 to terminate.";
cout << endl;
cin >> jumper;
if ( jumper == 9 ) return;
if ( jumper >=0 && jumper <= 4 )
jump_table[jumper]();
else
cout << "Invalid number" << endl;
}
};
};

jump A::jump_table = { A::jump_table = A0,
A::jump_table = A1,
A::jump_table = A2,
A::jump_table = A3,
A::jump_table = A4
};

int main(int argc, char**argv) {
A this_object;

this_object.run();
return 0;
}

```

Let's take a close look at what is going on here.

The first thing that you will notice is the function type definition at line 6. This states that our function type definition is a function that takes no parameters and returns no result.

In line 8, we have defined a table of functions of the type defined in line 6. Notice how this is declared as a static data member. Notice also, that we have changed the routines A0, A1, A2, A3 and A4 to static functions as well. This is necessary due to limitations imposed by the C++ language when dealing with addresses of class member functions.

The switch statement in the run() body has been removed and a few lines of code added to check that the range of the input provided by the user is 0 to 4 inclusive as well as handling the termination digit 9. Providing the digit is in the range 0 to 4, the function held in the jump_table entry is called in line 24.

Finally, the jump_table array is defined at the end of the class definition and each entry initialized with the address of it's corresponding class routine.

Is This A Good Question/Topic? 3

## Replies To: Jump Tables In Classes

### #2 chandanpatra Reputation: 0
• Posts: 9
• Joined: 02-August 10

Posted 22 March 2011 - 05:36 AM

Line No. 31 could have been like this:

``` jump A::jump_table[]={A0,A1,A2,A3,A4}; // It works

```

Why I simply cant initialize the static array items individually like the following way?

```// It doesn't work
jump A::jump_table=A0;
jump A::jump_table=A1;
jump A::jump_table=A2;
jump A::jump_table=A3;
jump A::jump_table=A4;

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }