use*_*453 2 c++ inheritance templates constructor class
我发布了一个非常相似的问题并得到了答案.我现在面临与构造函数相同的问题..如何编写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:
};
Run Code Online (Sandbox Code Playgroud)
您需要在初始化列表中调用基类构造函数T2
:
T2(int t) : T1<T>(t) {}
Run Code Online (Sandbox Code Playgroud)
T2<T>
的构造函数将调用T1<T>
构造函数,它将初始化T1<T>::m_t