C++ template specialization of constructor

后端 未结 7 906
醉话见心
醉话见心 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:53

    Assuming your really meant for A::test to be publicly accessible, you could do something like this:

    #include 
    
    
    template 
    struct ABase
    {
      ABase(int n) : test_( n > M )
      {}
    
      bool const test_;
    };
    
    
    template 
    struct A : ABase
    {
      A(int n) : ABase(n)
      {}
    };
    
    
    template 
    A::A(int n)
      : ABase<20>(n)
      { std::cerr << "One type" << std::endl; }
    

    Kick the tires:

    int main(int argc, char* argv[])
    {
      A a(19);
      std::cout << "a:" << a.test_ << std::endl;
      A b(31);
      std::cout << "b:" << b.test_ << std::endl;
      return 0;
    }
    

提交回复
热议问题