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

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

    Each specialisation of a class template gives a different class - they do not share any members with each other. Since you've explicitly specialised the entire class, you don't get any of the members from the template, and must implement them all.

    You can explicitly specialise individual members, rather than the entire class:

    template <> void D::pvf(){ cout << "bool type" << endl; }
    

    Then D will still contain all the members of the class template that you haven't explicitly specialised, including the default constructor.

提交回复
热议问题