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

标签 c++ class templates inheritance constructor

我发布了一个非常相似的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/

相关文章:

c++ - 是否可以使用 Visual Studio 构建 WinDBG 扩展?

jQuery 选择具有相同类的随机元素

class - 作为方法参数的 Swift 类变量

visual-studio-2008 - Visual Studio 自动包含 (C#)

c++ - 匹配多种类型以进行模板特化解析

c++ - 忽略模板特化并显式使用非特化模板 (std::vector<bool>)

c++ - "int f(int(fn)())"和 "int f(int(*fn)())"之间的区别?

c++ - 使用unique_lock锁定互斥锁时调用std::system_error

c# - 是否可以查看使用接口(interface)实例化的类方法的摘要?

c++ - 在 C++ 中创建类型未知的模板 vector (空模板)