1

When we create a template class example1 which derives from another template class example2. The template class example1 does not inherit systematically of example2 members.

Consider the code bellow:

template<class T>
class example2 {
protected:
    int a;
public:
    int getA() const {return a;}
};

template<class T>
class example1 : public example2<T> {
    T* p;
public:
    void test() {
        example2<T>::a = 8;
    }
};

in the method test of the class example1, we have to use example2<T>:: operator in order to be able to access to the member a. Otherwise, the complier generates an error: 'a' was not declared in this scope.

I mean, why the complier does not recognize a in the class example1? Why we should specify example2<T>::a and not just a?

Thanks

ThunderPhoenix
  • 1,649
  • 4
  • 20
  • 47

0 Answers0