小编Som*_*esh的帖子

在C++中从自身派生模板类

在我被要求使用的遗留代码的一部分中,我遇到了一个我不理解的概念.搜索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)

c++ inheritance templates

6
推荐指数
1
解决办法
781
查看次数

标签 统计

c++ ×1

inheritance ×1

templates ×1