#include #include using namespace std; class Command { int type; static int next; public: Command() { type = (next++ % 3); } int getType() { return type; } void fee() { cout << "fee "; } void phi() { cout << "phi "; } void pheaux() { cout << "pheaux "; } }; int Command::next = 0; class Queue { enum { SIZE = 10 }; Command* array[SIZE]; int add, remove; public: Queue() { add = remove = 0; } void enque( Command* c ) { array[add] = c; add = (add + 1) % SIZE; } Command* deque() { int temp = remove; remove = (remove + 1) % SIZE; return array[temp]; } }; void main( void ) { Queue que; Command input[6], *cmd; for (int i=0; i < 6; i++) que.enque( &input[i] ); for (i=0; i < 6; i++) { cmd = que.deque(); if (cmd->getType() == 0) cmd->fee(); else if (cmd->getType() == 1) cmd->phi(); else if (cmd->getType() == 2) cmd->pheaux(); } cout << '\n'; } // fee phi pheaux fee phi pheaux #if 0 class Command { static void (Command::*ptrs[])(); static int next; void (Command::*action)(); public: Command() { action = ptrs[(next++ % 3)]; } void fee() { cout << "fee "; } void phi() { cout << "phi "; } void pheaux() { cout << "pheaux "; } void execute() { (this->*action)(); } }; int Command::next = 0; void (Command::*Command::ptrs[])() = { Command::fee, Command::phi, Command::pheaux }; class Queue { enum { SIZE = 10 }; Command* array[SIZE]; int add, remove; public: Queue() { add = remove = 0; } void enque( Command* c ) { array[add] = c; add = (add + 1) % SIZE; } Command* deque() { int temp = remove; remove = (remove + 1) % SIZE; return array[temp]; } }; void main( void ) { Queue que; Command input[6], *cmd; for (int i=0; i < 6; i++) que.enque( &input[i] ); for (i=0; i < 6; i++) { cmd = que.deque(); cmd->execute(); } cout << '\n'; } // fee phi pheaux fee phi pheaux #endif