Som*_*esh 6 c++ inheritance templates
在我被要求使用的遗留代码的一部分中,我遇到了一个我不理解的概念.搜索SO和谷歌搜索没有太大帮助,因此这个问题.
有一个模板类,如下所示:
template<int Index_t, int Kind_t, ProtocolType Prot_t, class Protocol>
class CommandHandlerGeneric
: private CommandHandlerGeneric<Index_t-1, Kind_t, Prot_t, Protocol> {
public:
CommandHandlerGeneric(Protocol& Shared, CmdHandlerBase** Cont) :
CommandHandlerGeneric<Index_t-1, Kind_t, Prot_t, Protocol>(Shared, Cont) {}
};
Run Code Online (Sandbox Code Playgroud)
该CmdHandlerBase班的是,在不同的页眉其他地方存在非模板类.按照上面的定义,有一个宏看起来像这样:
#define REGISTER_COMMAND_HANDLER_BASE(CmdIndex, CmdType, CmdKind, ProtType) \
template<class Protocol> \
class CommandHandlerGeneric<CmdIndex, CmdKind, ProtType, Protocol>
: private CommandHandlerGeneric<CmdIndex-1, CmdKind, ProtType, Protocol> \
{ \
CmdType m_Cmd;\
public: \
CommandHandlerGeneric(Protocol& Shared, CmdHandlerBase** Cont) : \
m_Cmd(Shared), \
CommandHandlerGeneric<CmdIndex-1, CmdKind, ProtType, Protocol>(Shared, Cont) \
{ Cont[CmdIndex] = &m_Cmd; } \
};
Run Code Online (Sandbox Code Playgroud)
看起来上面的宏部分专门化了类模板CommandHandlerGeneric.它是否正确?从自己私下衍生课程背后的理由是什么?