在下面的代码中,为什么T2会出现此错误‘m_t’ was not declared in this scope,而TB是正常的?
如何在仍然使用模板的情况下访问T2中的T1成员?
// All good
class TA
{
public:
TA() {}
protected:
int m_t;
};
class TB : public TA
{
public:
TB() {}
int get()
{ return m_t; }
protected:
};
// Error in T2
template<typename T>
class T1
{
public:
T1() {}
protected:
int m_t;
};
template<typename T>
class T2 : public T1<T>
{
public:
T2() {}
int get()
{ return m_t; }
protected:
};
Run Code Online (Sandbox Code Playgroud) 我发布了一个非常相似的问题并得到了答案.我现在面临与构造函数相同的问题..如何编写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)