C++ template class specialization: why do common methods need to be re-implemented

后端 未结 5 787
心在旅途
心在旅途 2021-02-19 06:32

In the sample:

#include 

using namespace std;

class B
{
public:
    virtual void pvf() = 0;
};

template 
class D : public B
{
p         


        
5条回答
  •  孤街浪徒
    2021-02-19 07:19

    You need to reimplement it because D and D are totally unrelated classes (they just happen to "share the name"). That's just how templates work.

    If you want the classes to share construction code, just put that code inside B::B (i.e. the same thing you do every time you want to reuse code in different branches of the same hierarchy: move the code up and let inheritance handle the rest).

提交回复
热议问题