template inheritance c++

后端 未结 4 1268
天涯浪人
天涯浪人 2020-12-08 16:28

i am a new programmer in 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

相关标签:
4条回答
  • 2020-12-08 16:55

    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.

    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.

    0 讨论(0)
  • 2020-12-08 16:56

    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. This means that base classes that are dependent on the template parameter won't be searched to resolve the name p. To work around this issue you need to use something that does depend on the template parameter. Using this-> will do this.

    e.g.

    cout << this->p << endl;
    
    0 讨论(0)
  • 2020-12-08 17:01

    I don't get that compiler error in 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).

    0 讨论(0)
  • 2020-12-08 17:13

    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.

    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();
    };
    
    0 讨论(0)
提交回复
热议问题