|
|
如下,写了一个.h文件。里面声明了一个类Event和一个模板类EventHandler。
Event的方法我在.cpp文件里面实现了,但是好像不能把EventHandler的方法在.cpp里面写,写了的话虽然编译可过,但是实现连接的时候总是说无法解析的方法。 [em6]
现在请教一下这个问题,到底能不能把模板类的方法在.cpp里面实现?
- [color=#0000ff]#include[/color] <windows.h>
- [color=#0000ff]class[/color] Event
- {
- [color=#0000ff]public[/color]:
- [color=#0000ff]void[/color]* pSender;
- Event();
- Event([color=#0000ff]void[/color]* pSender);
- };
- [color=#0000ff]template[/color]<[color=#0000ff]class[/color] CLASSNAME>
- [color=#0000ff]union[/color] upEventHandlerFunction
- {
- [color=#0000ff]void[/color] (*pfuncEventHandlerFunction)(Event*);
- [color=#0000ff]void[/color] (CLASSNAME::*pmfuncEventHandlerFunction)(Event*);
- };
- [color=#0000ff]template[/color]<[color=#0000ff]class[/color] CLASSNAME>
- [color=#0000ff]class[/color] EventHandler
- {
- [color=#0000ff]private[/color]:
- CLASSNAME* pclass;
- upEventHandlerFunction<CLASSNAME> upHandlerFunction;
- [color=#0000ff]public[/color]:
- EventHandler(CLASSNAME* pclass,[color=#0000ff]void[/color] (CLASSNAME::*pmfunc)(Event*))
- {
- [color=#0000ff]this[/color]->pclass=pclass;
- upHandlerFunction.pmfuncEventHandlerFunction=pmfunc;
- }
- EventHandler([color=#0000ff]void[/color](*pfunc)(Event*))
- {
- pclass=NULL;
- upHandlerFunction.pfuncEventHandlerFunction=pfunc;
- }
- [color=#0000ff]void[/color] Invoke(Event* e)
- {
- [color=#0000ff]if[/color](pclass==NULL)
- {
- (*(upHandlerFunction.pfuncEventHandlerFunction))(e);
- }
- [color=#0000ff]else[/color]
- {
- [color=#0000ff]try[/color]
- {
- (pclass->*(upHandlerFunction.pmfuncEventHandlerFunction))(e);
- }
- [color=#0000ff]catch[/color](...){}
- }
- }
- };
复制代码 |
|