a.h
template <typename T>
class A
{
public:
int a;
}
b.h
template <typename T>
class B : public A<T>
{
public:
int f();
}
template <typename T>
int B<T>::f()
{
int t;
t = this->a; //Okay
t = a //Error
return 0;
}
why does error happen when I don’t use this->?
Can I omit this-> with using some method?
(I fixed some mistakes)
There are two phases in template instantiation (“Two Phase Name Lookup”).
In the first phase, all non-dependent names are resolved (looked up). In the second phase, dependent names are resolved.
A dependent name is a name which depends on a template parameter, e.g.:
Now, you write:
This is exactly what I described. In the okay term,
tis looked up in phase 2,because
thisdepends on a template parameter.The errorful term is looked up in phase 1, because nothing in that name depends on a template parameter.
But in phase 1, no
ais visible, because the compiler cannot introspect base class templatesin phase 1, because templates can be specialized and at the point of instantiation,
which can be remote from the primary template declaration, another specialization
that has no
a, might be visible.Example:
Btw, I have once written this-> is not only a matter of style in my very low frequency blog.