Page 1 of 1

Jump Tables In Classes Rate Topic: -----

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • 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[5];
		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[5] = { A::jump_table[0] = A0, 
                          A::jump_table[1] = A1, 
                          A::jump_table[2] = A2, 
                          A::jump_table[3] = A3, 
                          A::jump_table[4] = 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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[0]=A0;
jump A::jump_table[1]=A1;
jump A::jump_table[2]=A2;
jump A::jump_table[3]=A3;
jump A::jump_table[4]=A4;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1