简体   繁体   English

模板继承c ++

[英]template inheritance c++

i am a new programmer in c++. 我是c ++的新程序员。 and i am using templates for the first time. 我第一次使用模板。

i have an abstract class and another class extending it. 我有一个抽象类和另一个扩展它的类。 but all the protected members of the abstract class are not recognised by the other class: 但是抽象类的所有受保护成员都不被其他类识别:

class0.h: class0.h:

template<class T>
class class0 {

protected:
    char p;
public:
    char getChar();
};

**class1.h**
template<class T>
class class1:public class0<T> {
public:
    void printChar();
};
template<class T>
void class1<T>::printChar(){
    cout<< p<<endl;//p was not declared in this scope
}

thank you. 谢谢。 have a great week =) 有一个伟大的一周=)

The reason that this is happening is to do with the lookup rules for templates. 发生这种情况的原因是与模板的查找规则有关。

p isn't a dependent expression because it is just an identifier and not something that depends on the template parameter. p不是依赖表达式,因为它只是一个标识符,而不是依赖于模板参数的东西。 This means that base classes that are dependent on the template parameter won't be searched to resolve the name p . 这意味着将不会搜索依赖于模板参数的基类来解析名称p To work around this issue you need to use something that does depend on the template parameter. 要解决此问题,您需要使用取决于模板参数的内容。 Using this-> will do this. 使用this->会这样做。

eg 例如

cout << this->p << endl;

For a name to be looked up in a dependent base class, two conditions need to be satisfied 要在依赖基类中查找名称,需要满足两个条件

  • It's necessary that the lookup is not unqualified 这是必要的查找是不是不合格
  • It's necessary that the name is dependent 名称必须依赖

These rules as stated in C++03 are different from the rules stated by unrevised C++98 , where satisfying the second bullet (making a name dependent) was sufficient for finding names declared in dependent base classes. C ++ 03中所述的这些规则与未经修改的C ++ 98规定的规则不同,后者满足第二个项目符号(使名称相关) 足以查找在从属基类中声明的名称。

A dependent name is looked up at instantiation time and a lookup other than unqualified lookup will not ignore dependent base classes. 在实例化时查找依赖名称,并且除非非限定查找之外的查找不会忽略依赖基类。 Both of these conditions need to be satisfied to find a name declared in a dependent base class, neither of them alone is sufficient . 需要满足这两个条件才能找到在依赖基类中声明的名称, 单独使用它们都不够 To satisfy both conditions you can use various constructs 要满足这两个条件,您可以使用各种结构

this->p
class1::p

Both names p are dependent and the first version uses class member access lookup and the second version uses qualified name lookup . 两个名称p都是相关的,第一个版本使用类成员访问查找 ,第二个版本使用限定名称查找

I don't get that compiler error in VC9. 我在VC9中没有得到编译器错误。 However, there are several problems with the code: First, it doesn't need to be a template class as it's currently written...but maybe you just simplified it for this question? 但是,代码有几个问题:首先,它不需要是一个模板类,因为它是当前编写的...但是你可能只是为这个问题简化了它? Second, The base class should have a virtual destructor. 其次,基类应该有一个虚拟析构函数。

#include <iostream>

using namespace std;

class class0 {
public:
   virtual ~class0(){}

protected:
    char p;
public:
    char getChar();
};

class class1 : public class0 {
public:
    void printChar();
};

void class1::printChar(){
    cout << p << endl;//p was not declared in this scope
}

int main() {
   class1 c;
   c.printChar();
   return 1;
}

Since you're learning about templates, I would suggest not mixing concepts (inheritance & templates) while learning. 由于您正在学习模板,我建议您在学习时不要混合概念(继承和模板)。 Start with a simple example like this... 从这样一个简单的例子开始......

#include <iostream>
#include <string>

using namespace std;

template <typename T>
T add(const T& a, const T& b) {
   return a + b;
}

int main() {
   int x = 5;
   int y = 5;

   int z = add(x, y);
   cout << z << endl;

   string s1("Hello, ");
   string s2("World!");

   string s3 = add(s1, s2);
   cout << s3 << endl;

   return 1;
}

The important concept in the code above is that we wrote ONE function that knows how to add integers and strings (and many other types for that matter). 上面代码中的重要概念是我们编写了一个知道如何添加整数和字符串(以及其他许多类型)的函数。

Sorry for reviving such an old question, but I just wanted to add this thing which i find valuable if you have lots and lots of "p" in your member functions. 很抱歉恢复这么老的问题,但我只是想添加一些我觉得有价值的东西,如果你的成员函数中有很多很多“p”。

class class1:public class0<T> {
public:
    using class0<T>::p; // add this line and in all member functions 
                        // will assume that "p" is the p from class0<T>
                        // very useful if you have hundreds of "p":s
                        // and don't want to replace every single one with "this->p"
    void printChar();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM