#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class Widget { static int counter; public: Widget() { counter++; cout<<"my id is:"<<counter<<endl; cout<<"this is one day i come to this world"<<endl; } virtual ~Widget() { counter--; cout<<"there 's on day i will leave from this world"<<endl; } void ShowInfor() { cout<<"my id is:"<<counter<<endl; } void Running() { cout<<"i can running fast enough to reach the moon"<<endl; } }; //typedef void (Widget::*fun)() ; int Widget::counter=0; //template<class Function> class function { private: typedef void (Widget::*Fun)(); Fun fun; public: function(Fun fun) { this->fun=fun; } void operator()(Widget *wid) { (wid->*fun)(); } }; void Destroy(Widget *wid) { delete wid; } int main() { vector<Widget*> widgets; for(int i=0; i<10; i++) { widgets.push_back(new Widget()); } for_each(widgets.begin(),widgets.end(),function(&Widget::ShowInfor)); for_each(widgets.begin(),widgets.end(),function(&Widget::Running)); return 0; }