c ++模板继承方案 [英] c++ template inheritance scheme

查看:234
本文介绍了c ++模板继承方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wonder if the way I code this is correct. Can I create a template claas that inherits from a template class ? If I can, is the following code correct :

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-> 来引用属于同一个类的参数或方法吗?

Why are the this-> mandatory in the class B but not in the class C ? And in general, should I always add this-> to reference arguments or methods that belong to the same class ?

推荐答案

这在C ++ 03和C ++ 11标准的14.6.2p3部分有详细说明:

This is specifically addressed in section 14.6.2p3 of the C++03 and C++11 standards:


在类模板或类模板的成员的定义中,如果类模板的基类依赖于模板参数,则在未限定名称查找期间不会检查基类范围类模板或成员的定义,或在类模板或成员的实例化期间。

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 ++模板继承方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆