#include #include #include using namespace std; class Functor { public: virtual void operator() () = 0; }; template class TFunctor : public Functor { public: TFunctor(T* object, void(T::*f)()) : object(object), f(f) {} virtual void operator() () { (*object.*f)(); } private: T * object; void (T::*f) (); }; class File { public: void rename(string newName) { name = newName; } vector getActions(); void performAction(string action) { (*actions[action])(); } protected: File(string name) : name(name) { } map actions; string name; }; vector File::getActions() { vector ret; map::iterator i; for (i = actions.begin(); i != actions.end(); i++) ret.push_back(i->first); return ret; } class Avi : public File { public: Avi(string name) : File(name) { actions["view"] = new TFunctor(this, &Avi::view); } ~Avi() { delete actions["view"]; } void view() { cout << "Avi : View : " << name << endl; } }; class Txt : public File { public: Txt(string name) : File(name) { actions["edit"] = new TFunctor(this, &Txt::edit); actions["print"]= new TFunctor(this, &Txt::print); } ~Txt() { delete actions["edit"]; delete actions["print"]; } void edit() { cout << "Txt : Edit : " << name << endl; } void print() { cout << "Txt : Print : " << name << endl; } }; void performAllActions(File * f) { vector actions = f->getActions(); vector::iterator it; for (it = actions.begin(); it != actions.end(); it++) f->performAction(*it); } int main() { File * avi = new Avi("MyMovie.avi"); File * txt = new Txt("MyTxtFile.avi"); performAllActions(avi); performAllActions(txt); return 0; }