Template specialization and inheritance

后端 未结 3 929
北荒
北荒 2021-02-01 04:21

Suppose I have a template class with a lot of functions and I want to specialize them to change only a few of them and keep the other ones exactly as specified in the base templ

3条回答
  •  悲哀的现实
    2021-02-01 04:57

    You just have to use two template classes:

    template
    struct CommonBase
    {
      void print1() {cout << "Base::print1" << endl;};
      void print2() {cout << "Base::print2" << endl;};
    };
    
    template
    struct Base : public CommonBase
    {
    };
    
    template<>
    struct Base : public CommonBase
    {
      void print2() {cout << "Base::print2" << endl;};
    };
    

    You always use Base, rather than CommonBase.

提交回复
热议问题