C++ template specialization of constructor

后端 未结 7 914
醉话见心
醉话见心 2021-02-05 17:15

I have a templated class A and two typedefs A and A. How do I override the constructor for A ? The following does not wor

7条回答
  •  情话喂你
    2021-02-05 17:46

    The only thing you cannot do is use the typedef to define the constructor. Other than that, you ought to specialize the A constructor like this:

    template<> A::A(int){}
    

    If you want A to have a different constructor than the generic A, you need to specialize the whole A class:

    template<> class A {
    public:
       A(const string& takethistwentytimes) { cerr << "One Type" << std::endl; }
    };
    

提交回复
热议问题