473,513 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inherited member name qualification for templated class

Greetings,

The following code:

<code>

template<typename T>
class A
{
protected:
T n;
};

template<typename T>
class B : public A<T>
{
public:
void foo()
{
n = 1; // <--------- error
}
};

int main()
{
B<int> b;
b.foo();

return 0;
}

</code>

generates the error:

test.cpp: In member function `void B<T>::foo()':
test.cpp:15: error: `n' undeclared (first use this function)

If the base class member n is qualified with the full templated base
class name, as in:

A<T>::n = 1; // <--------- ok

it compiles fine. Also, for /non/ templated classes it is not necessary
to qualify the base class member name.

Could the compiler not infer from the templated class declarations that
the n in foo() refers to A<T>::n ?

Problem is, that in my real-world code there are (at least) four
template parameters and *many* inherited base class members; having to
quote the full templated base class name for every reference to an
inherited base class member would make my code incredibly clunky and
utterly unreadable. Is there a workaround for this?

Regards,

--
Lionel B

Jul 23 '05 #1
4 1847
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least) four
template parameters and *many* inherited base class members; having to
quote the full templated base class name for every reference to an
inherited base class member would make my code incredibly clunky and
utterly unreadable. Is there a workaround for this?


Have you tried using 'this->'? The pun is not intended.

V
Jul 23 '05 #2
Victor Bazarov wrote:
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least)
four template parameters and *many* inherited base class
members; having to quote the full templated base class name
for every reference to an inherited base class member would
make my code incredibly clunky and utterly unreadable. Is
there a workaround for this?


Have you tried using 'this->'? The pun is not intended.


Yes that does work... but it then leaves my code littered with
"this->"es. Not as bad as base_class<foo_type, bar_type, baz_type,
....>::myvar all over the place, perhaps, but still pretty ugly.

I am still a bit puzzled as to why the compiler can't seem to resolve
the name. Can't think of a situation where such a construct might be
ambiguous - and even if there is one, my case is surely unambiguous?

--
Lionel B

Jul 23 '05 #3
Lionel B wrote:
Victor Bazarov wrote:
Lionel B wrote:
[...]
Problem is, that in my real-world code there are (at least)
four template parameters and *many* inherited base class
members; having to quote the full templated base class name
for every reference to an inherited base class member would
make my code incredibly clunky and utterly unreadable. Is
there a workaround for this?


Have you tried using 'this->'? The pun is not intended.

Yes that does work... but it then leaves my code littered with
"this->"es. Not as bad as base_class<foo_type, bar_type, baz_type,
...>::myvar all over the place, perhaps, but still pretty ugly.

I am still a bit puzzled as to why the compiler can't seem to resolve
the name. Can't think of a situation where such a construct might be
ambiguous - and even if there is one, my case is surely unambiguous?


This has been discussed many times. The base classes are not searched
during name lookup if the base classes are dependent. It is often the
case with templates. See "dependent name lookup" for more information.

V
Jul 23 '05 #4
Victor Bazarov wrote:
Lionel B wrote:
Victor Bazarov wrote:
[...]


This has been discussed many times. The base classes are not
searched during name lookup if the base classes are dependent.
It is often the case with templates. See "dependent name
lookup" for more information.


Thanks, it's there in the FAQ (35.12 - didn't know what to look for -
and yes, it did make my head hurt). Seems like a feasible solution to
my situation might be a "using" statement:

template<typename T>
class B : public A<T>
{
using A<T>::n;
public:
void foo()
{
n = 1;
}
};

Cheers,

--
Lionel B

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3620
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
9
2297
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
6
1514
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
6
4394
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
3
2902
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
1
1482
by: cpunerd | last post by:
Hello, I'm not new to C++, but for some reason, until now I'd never had a need for deriving templated classes. Now though, I find myself seeing a weird problem. If I have a templated base class...
7
3783
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
15
7839
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
4
5535
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T>...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
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
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
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...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.