473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with inheritance of classes with template parameter

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
2 1467
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: arch | last post by:
Hi, I am trying to implement a class hierarchy where there will be a base class defining the interface and some common operations (interface+implentation): Constraints : no STL. This will be...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
8
by: Ben Pope | last post by:
Hi everybody, I know this is not usually the place for design questions, but I was wondering what some of you more experienced people would do. Say I have an application that has a C++ & STL...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
4
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.