// header.h
#ifndef __main_H__
#define __main_H__
#include "StdAfx.h"
template<class T1, class T2>
class C{
public:
typedef void (T1::*TemplateFun)(T2);
public:
C(T1 *t1, TemplateFun t2)
:_t1(t1), _t2(t2){}
void invoke( T2 param ){
cout << "T1:" << typeid(T1).name() << endl;
cout << "T2:" << typeid(_t2).name() << endl;
(_t1->*_t2)(param);
}
private:
T1 *_t1;
TemplateFun _t2;
};
#endif
#include "header.h"
class Test{
public:
void f( int a ){ cout << "Test:" << a << endl; }
};
int main(){
Test *t = new Test;
C<Test, int> c(t, &Test::f );
c.invoke(3);
delete t;
}
/*
T1:class Test
T2:void (__thiscall Test::*)(int)
Test:3
请按任意键继续. . .
*/
类模板运用之实现委托类
最新推荐文章于 2020-05-25 21:26:28 发布