我想知道我的编码方式是否正确。我可以创建一个继承模板类的模板类吗?如果可以的话,以下代码是否正确:
template<typename Type>
class A{
public:
A(){};
method_A(){//do whatever}
protected:
int a;
}
第二类是:
template<typename Type>
class B:public<Type> A {
public:
B(){};
method_B(){this->a=0; this->method_A();}
protected:
int b;
}
我的最后一个类是:
class C:public<double> B{
public:
C(){};
method_C(){ b = 0; method_B();}
protected:
int c;
}
为什么 this->
在 B 类中是必需的,而在 C 类中却不是?一般来说,我应该始终添加 this->
来引用属于同一类的参数或方法吗?
最佳答案
C++03 和 C++11 标准的第 14.6.2p3 节专门解决了这一问题:
In the definition of a class template or a member of a class template, if a base class of the class template depends on a template parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.
关于C++ 模板继承方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772960/