Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- void f_a(void)
- {
- std::cout << "a";
- }
- void f_b(void)
- {
- std::cout << "b";
- }
- void f_c(void)
- {
- std::cout << "c";
- }
- void f_d(void)
- {
- std::cout << "d";
- }
- void f_e(void)
- {
- std::cout << "e";
- }
- void f_f(void)
- {
- std::cout << "f";
- }
- class Printer
- {
- private:
- void (*pfnFunc)(void);
- public:
- void setFunction(void (*pfnFunc)(void))
- {
- this->pfnFunc = pfnFunc;
- }
- void exec()
- {
- this->pfnFunc();
- }
- void (*getFunction(void))(void)
- {
- return this->pfnFunc;
- }
- static void (*getFunctionByID(int i))(void)
- {
- void (*functions[])(void) = { f_a, f_b, f_c, f_d, f_e, f_f };
- return functions[i];
- }
- };
- int main ( void )
- {
- srand(time(NULL));
- Printer printer;
- for(int i = 0; i < 1000; i++) {
- printer.setFunction(Printer::getFunctionByID(rand() % 5));
- printer.exec();
- }
- std::cin.get();
- }
Add Comment
Please, Sign In to add comment