我发布了一个非常相似的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:
};
最佳答案
您需要在T2
的初始化列表中调用基类构造函数:
T2(int t) : T1<T>(t) {}
T2<T>
的构造函数将调用 T1<T>
的构造函数,它将初始化 T1<T>::m_t
关于c++ - 构造函数中模板化类成员的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16524739/