比如, 我有 void abc (ClassA a, int b) void bbc(ClassB a,int b) void bcc (ClassC a, int b) ......等几十个这样的函数,要把他们当参数condition传递给 Execute(ConditionDelegate<T> condition );
那应该怎么写? 有人说用模板,但是模板只能是名称一样的函数。 [解决办法]
模板的功能比你说的强多了。
template <typename Result,typename Arg> Result exe(Result (*pfun)(Arg,int)){ Result res = pfun(Arg(),3); return res; }
int fun(char c, int i){ return i + 3; }
int main(){
printf("%d",exe(fun)); system("pause"); return 0;
}
[解决办法] 用模板+虚函数可以实现.
实现的效果如下: [code] class ClassA {}; class ClassB {}; class ClassC {};
void abc (ClassA a, int b) { printf("%s: %d\n", typeid(a).name(), b); }
void bbc(ClassB a,int b) { printf("%s: %d\n", typeid(a).name(), b); }
void bcc (ClassC a, int b) { printf("%s: %d\n", typeid(a).name(), b); }