By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Problem with inheritance of classes with template parameter

P: n/a
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

Aug 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thomas Witkowski wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 6 '07 #2

P: n/a
On Aug 6, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Thomas Witkowski wrote:
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.
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.

--
James Kanze (GABI Software) email:james.kanze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.