Show oop.cc syntax highlighted
#include <map>
#include <string>
#include <iostream>
using namespace std;
class A
{
public:
A() {
if(!map_initialized) {
cmd["a"] = &A::a;
cmd["b"] = &A::b;
cmd["c"] = &A::c;
}
}
virtual void a() { cout << "Method A::a called." << endl; }
virtual void b() { cout << "Method A::b called." << endl; }
virtual void c() { cout << "Method A::c called." << endl; }
void exec(string c) {
(this->*cmd[c])();
}
private:
static map<string, void (A::*)()> cmd;
static bool map_initialized;
};
class B : public A
{
public:
void a() { cout << "Method B::a called." << endl; }
};
bool A::map_initialized = false;
map<string, void (A::*)()> A::cmd;
int main()
{
A t;
A *p = new B();
t.exec("a");
t.exec("b");
t.exec("c");
p->exec("a");
return 0;
}
See more files for this project here