forked from elwin0214/note
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.cpp
More file actions
63 lines (51 loc) · 1.22 KB
/
bind.cpp
File metadata and controls
63 lines (51 loc) · 1.22 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
//a bind function example
#include <iostream>
template<typename ReturnType,typename ArgType>
class BaseFunction{
public:
virtual ReturnType operator()(ArgType arg)=0;
};
template<typename Class,typename ReturnType,typename ArgType>
class MemberFunction:public BaseFunction<ReturnType,ArgType>{
private:
typedef ReturnType (Class::*Func)(ArgType);
Func func;
Class *obj;
public:
MemberFunction(Class *obj,Func func):obj(obj),func(func){}
ReturnType operator()(ArgType arg)
{
return (obj->*func)(arg);
}
};
template<typename ReturnType,typename ArgType>
class Function{
private:
BaseFunction<ReturnType,ArgType> *func;
public:
Function(BaseFunction<ReturnType,ArgType> *func):func(func){}
ReturnType operator()(ArgType arg)
{
return func->operator()(arg);
}
};
class Test{
public:
int say(int h){
std::cout<<"say"<<std::endl;
return 1;
}
};
template<typename Class,typename ReturnType,typename ArgType>
Function<ReturnType,ArgType> bind(ReturnType (Class::*func)(ArgType),Class *obj)
{
return new MemberFunction<Class,ReturnType,ArgType>(obj,func);
}
int main(){
Test t;
int i=2;
//MemberFunction<Test,int,int> b(&t,&Test::say);
Function<int,int> func= bind<Test,int,int>(&Test::say,&t);
func(1);
return 0;
}