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

后端 未结 5 805
心在旅途
心在旅途 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:20

    No, there is not.

    Specialization is behaving very differently than inheritence. It has no connection to the generic template version.

    When you use/instantiate a template, the compiler will create a new type name, and then look for how this type is defined. When it finds a specialization, then it takes that as the definition for the new type. When it does not, it takes the generic template and instantiates it.

    Therefore they have no connection, and you are just writing an entirely new class, just with a special name for the compiler to find in case of someone using/instantiating the template to find it under that name.

提交回复
热议问题