类成员的继承,与模板混合
Inheritance of class members, mixed with templates
在下面的代码中,为什么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:
};
您需要使用this->m_t
使其成为依赖名称。编译模板时,分两个阶段查找名称。非依赖名称在编译器第一次解析模板时查找。依赖名称在模板实例化时查找。将其更改为this->m_t
将延迟查找,直到get
函数实际实例化之后,在这种情况下,基类类型已知,编译器可以验证成员的存在。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 参数的混合值,当我调用指针成员函数时
- C - 混合默认成员初始化器和成员初始化列表 - 坏主意
- 混合共享/静态库时静态成员的多个副本
- 混合类型的Boost运算符-转换和私有成员
- 混合模板/非模板继承分类和成员继承
- MinGW 4.7.0 到 4.7.2 错误:使用混合虚拟和非虚拟多重继承时成员函数中的"this"指针无效
- 混合代码-函数成员指针
- 类成员的继承,与模板混合
- c++可变模板构造函数的成员和混合的初始化列表