2013-05-13 92 views
2

我发布了一个非常类似的question并得到了我的答案。我现在面临与构造函数相同的问题.. 如何写一个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: 
}; 

回答

9

您需要调用基类的构造函数初始化列表为T2

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

T2<T>的构造函数会调用T1<T>的构造,这将初始化T1<T>::m_t

+0

谢谢!我应该想到这一点.. – user2287453 2013-05-13 14:49:20