我写了一个Template Class ,目的是对CWinThread 进行封装,但是,我的template class的InitInstance 和  ExitInstance 永远不会执行道,为何?
//CTThread.h:#pragma once
template <class T>
class CTThread : public T
{
public:
CTThread()
{
};
virtual ~CTThread()
{
};
virtual BOOL InitInstance()
{ TRACE0("InitInstance \n");
return T::InitInstance();
};
virtual int ExitInstance()
{
TRACE0("ExitInstance ");
return T::InitInstance();
};
void Foo()
{
//do something
;
};
private:
//member.
};使用:
CRuntimeClass* prtt = RUNTIME_CLASS( CTThread<CWndThread> );
::AfxBeginThread((CRuntimeClass*)prtt,THREAD_PRIORITY_NORMAL,0,0,NULL);
从结果看,线程是创建成功了,但是呢,template class 的 InitInstance 和 ExitInstance 不会执行.
高手们,如何解释和解决?

解决方案 »

  1.   

    CRuntimeClass* prtt = RUNTIME_CLASS( CTThread<CWndThread> );
                                                  ~~~~~~~~~改称CWinThread
      

  2.   

    CWinThread 不是一个模板类,她如何能够带<CWndThread>??
      

  3.   

    你的CTThread不是要从CWinThread派生吗?当然要把CWndThread改成CWinThread。像你那样写能通过编译吗?
      

  4.   

    哦,你是说,后面的 CWndThread, 她是我写的一个从CWinThread继承来的 一个测试用窗口线程类,如果直接用CWinThread, 她不就失去了它的特质了吗?
      

  5.   

    这样做可以编译成功??怀疑RUNTIME_CLASS( CTThread<CWndThread> ); // 这段代码被宏扩展为 
        // ((CRuntimeClass*)(&CTThread<CWndThread>::classCTThread<CWndThread>)) 
      

  6.   

    但是它确实编译成功了,并且运行了,只是没有运行template class的InitInstance ,ExitInstance