您的位置:首页 > 其它

.*操作符的用法

2010-11-23 13:27 113 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: