当前位置: 代码迷 >> 编程 >> 种模板运用之实现委托类
  详细解决方案

种模板运用之实现委托类

热度:5023   发布时间:2013-02-26 00:00:00.0
类模板运用之实现委托类
// 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 TestT2:void (__thiscall Test::*)(int)Test:3请按任意键继续. . .*/
强大的设计模式啊,超方便的说。。。



  相关解决方案