C++ 模板继承方案

标签 c++ class templates inheritance

我想知道我的编码方式是否正确。我可以创建一个继承模板类的模板类吗?如果可以的话,以下代码是否正确:

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/

相关文章:

c++ - 引用重新分配的变量

c++ - 通过指针访问数组

python - 将模块内的所有函数和类导入到类python中

c++ - 特定类模板类型的模板化类方法的不同实现

java - Java 类智能模板引擎?

c++ - 尝试使用 math.h 在 Qt 中访问 fmin 和 fmax

c++ - 变量 '...' 周围的堆栈已损坏

python - 如何让这个程序在 Python 中绘制 10 个椭圆?

java在第二类中找不到构造函数

c++ - VS 2012 模板错误