Why do I need to use region resolution in template inheritance?

If I work with classes, everything is just fine:

struct Base1 {
  int value;
  Base1(int value) : value(value) { }
};

struct Test1 : public Base1 {
  int getValue() { return value; }  
  Test1(int value) : Base1(value) { }
};

but with the resolution of the template area you must:

template <typename T>
struct Base {
  T value;
  Base(T value) : value(value) { }
};

template <typename T>
struct Test : public Base<T> {
  typedef Base<T> parent;
  T getValue() { return parent::value; }  // why do I need to use parent:: here?
  Test(T value) : parent(value) { }
};

Without scope permission, I get an error 'value' was not declared in this scope(using the gcc compiler). Why?

+3
source share
2 answers

Since the compiler does not know what valuedepends on the template argument. Thus, he tries to resolve it during the first pass (before creating the template instance) and does not work.

These two options are designed to use the resolution of the shooting, as you have, or use this->value. Since thisit is always a dependent name, this will cause the assessment to pass during the second pass.

. http://ideone.com/07odY

: :

Test<T> Base<T>, - Base<std::string> () Base<T> value, . , , , .

+6

( Test), value . , "! , , value T."

+3

All Articles