Problem with inheritance of classes with template parameter

  • Thread starter Thomas Witkowski
  • Start date
T

Thomas Witkowski

Hello,

I have a class with one template parameter. When a second class, also
with a template parameter that is forwarded to the first class, is
derived from this class, why it is required to enter the variables of
the first class with this-> ? Here a small example:

template<typename T>
class Class1
{
public:
bool testvar;
};

template<typename T>
class Class2 : public Class1<T>
{
public:
void test123() {
this->testvar = false;
}
};

If I delete this->, so I would just write testvar = false, it does not
work. The compiler (gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52))
returns the error that testvar is not in the current scope. Can
somebody explain it to me?

Regards,

Thomas
 
V

Victor Bazarov

Thomas said:
I have a class with one template parameter. When a second class, also
with a template parameter that is forwarded to the first class, is
derived from this class, why it is required to enter the variables of
the first class with this-> ?

Because such are the rules of the language. The name lookup is only
performed in the non-dependent [temp.dep]/3 base classes.
Here a small example:

template<typename T>
class Class1
{
public:
bool testvar;
};

template<typename T>
class Class2 : public Class1<T>
{
public:
void test123() {
this->testvar = false;
}
};

If I delete this->, so I would just write testvar = false, it does not
work. The compiler (gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52))
returns the error that testvar is not in the current scope. Can
somebody explain it to me?

I am not sure what to explain. You have to tell the compiler that the
name you're using ('testvar') is a member. Since that name is not found
in the 'Class2' itself, the compiler cannot assume whether you meant to
use the member from the base class or a global object (if the base class
does not have such a member, which is easily achieved with a "custom"
specialisation of 'Class1' template).

V
 
J

James Kanze

Because such are the rules of the language. The name lookup is only
performed in the non-dependent [temp.dep]/3 base classes.
Here a small example:
template<typename T>
class Class1
{
public:
bool testvar;
};
template<typename T>
class Class2 : public Class1<T>
{
public:
void test123() {
this->testvar = false;
}
};
If I delete this->, so I would just write testvar = false, it does not
work. The compiler (gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52))
returns the error that testvar is not in the current scope. Can
somebody explain it to me?
I am not sure what to explain. You have to tell the compiler that the
name you're using ('testvar') is a member.

More strictly speaking, you have to tell the compiler that the
symbol depends on the arguments to the template. There are
several ways to do this; which one is appropriate depends on the
context.
Since that name is not found
in the 'Class2' itself, the compiler cannot assume whether you meant to
use the member from the base class or a global object (if the base class
does not have such a member, which is easily achieved with a "custom"
specialisation of 'Class1' template).

But the compiler does assume it. It assumes that you wanted the
symbol bound at the definition point of the template,
independently of any additional symbols available at the
instantiation point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top