小编use*_*453的帖子

类成员的继承,与模板混合

在下面的代码中,为什么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)

c++ inheritance templates member

3
推荐指数
1
解决办法
129
查看次数

构造函数中模板化类成员的继承

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

c++ inheritance templates constructor class

2
推荐指数
1
解决办法
70
查看次数

标签 统计

c++ ×2

inheritance ×2

templates ×2

class ×1

constructor ×1

member ×1