user2287453
user2287453

Reputation: 149

Inheritance of templated class members in constructor

I posted a very similar question and got my answer. I'm now facing the same issue with the constructor.. How would one write the constructor for T2 ?

template<typename T>
class T1
{
    public:
      T1(int t) : m_t(t) {}

    protected:
    int m_t;
};

template<typename T>
class T2 : public T1<T>
{
    public:
      T2(int t) : m_t(t) {} // error

      int get()
      { return this->m_t; }

    protected:
};

Upvotes: 2

Views: 79

Answers (1)

Charles Salvia
Charles Salvia

Reputation: 53339

You need to call the base class constructor in the initializer list for T2:

T2(int t) : T1<T>(t) {}

T2<T>'s constructor will call T1<T>'s constructor, which will initialize T1<T>::m_t

Upvotes: 9

Related Questions