-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.cpp
More file actions
66 lines (58 loc) · 1.21 KB
/
Copy pathcallback.cpp
File metadata and controls
66 lines (58 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
*\author peakflys
*\brief C++方式实现的C++回调
*/
#include <iostream>
#include <vector>
#include <tr1/functional>
#define COUNTARRAY(a) sizeof(a)/sizeof(a[0])
class User
{
public:
User(std::string _name,bool _online) : name(_name),online(_online)
{
}
const std::string& getName() const
{
return this->name;
}
bool checkOnLine()
{
return online;
}
private:
std::string name;
bool online;
};
class Test
{
public:
Test(const std::vector<User>& _userList) : userList(_userList)
{
}
void static print(const User& user);
void execEvery(std::tr1::function<void (const User&)> func);
private:
std::vector<User> userList;
};
void Test::print(const User& user)
{
std::cout<<user.getName()<<" ";
}
void Test::execEvery(std::tr1::function<void (const User&)> func)
{
for(std::vector<User>::iterator it=userList.begin();it!=userList.end();++it)
{
if((*it).checkOnLine())
func(*it); //注意格式
}
std::cout<<std::endl;
}
int main()
{
User um[] = {User("张三",true),User("李四",false),User("王二",true),User("麻子",true)};
std::vector<User> vu(um,um+COUNTARRAY(um));
Test t(vu);
t.execEvery(std::tr1::bind(Test::print,std::tr1::placeholders::_1));
return 0;
}