473,466 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Deriving from a templated base class

Hello,

I have the following code
---
template < typename T>
struct Base
{
T base_member;
};

template < typename T>
struct Child: public Base <T>
{
T myfunc(){ return base_member; }
};

int main(){
Child<intchild;
return 0;
}
---
To my surprise it does not appear to compile (using g++ 3.4):
temptest1.cpp: In member function `T Child<T>::myfunc()':
temptest1.cpp:10: error: `base_member' was not declared in this scope

However changing the myfunc() line to

T myfunc(){ return Base<T>::base_member; }

makes it work fine. Does anyone know why i have to explicitly define
the namespace ? I don't understand why it should be ambigous which
base_member i mean, since there's only one base class ? Is there a way
to make it work implicitly ?

Thanks for any help,

Mike

Oct 28 '06 #1
2 1574

m.****@gmx.net wrote:
Hello,

I have the following code
---
template < typename T>
struct Base
{
T base_member;
};

template < typename T>
struct Child: public Base <T>
{
T myfunc(){ return base_member; }
};

int main(){
Child<intchild;
return 0;
}
---
To my surprise it does not appear to compile (using g++ 3.4):
temptest1.cpp: In member function `T Child<T>::myfunc()':
temptest1.cpp:10: error: `base_member' was not declared in this scope

However changing the myfunc() line to

T myfunc(){ return Base<T>::base_member; }

makes it work fine. Does anyone know why i have to explicitly define
the namespace ? I don't understand why it should be ambigous which
base_member i mean, since there's only one base class ? Is there a way
to make it work implicitly ?

Thanks for any help,

Mike
As you've noticed, the compiler does not verify the base class for
non-dependant names.
If you plan to claim that base_member *is* dependant on T. Its not.
At least, not as far as the template name lookup is concerned.

To illustrate: If you add the following to the base class:

const T& get() const { return base_member; }
// or
T get() const { return base_member; }

those too need the qualifier, but...

const T& get(T t) const { return base_member; } // silly example, t is
ignored
// or
void set(const T& t) { base_member = t; }

these would *not* need the Base<T>:: qualifier since get(...) and
set(...) are dependants (their signatures are dependant on T).
IMHO, doesn't it make sense to qualify them anyways?

Oct 28 '06 #3

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
4
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
11
by: Matthias | last post by:
Hello, templated virtual member functions are not allowed. Now I am searching for a good workaround for this problem, but I can't find any. Here's my problem: class Base { template...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
3
by: Rickarazzi | last post by:
This is a problem that arose while using GNU G++ 3.4.5 under Linux. The problem is: How to get a pointer value from a templated object inside a class? Normally, I would add an '&' can carry on. ...
4
by: Jim Langston | last post by:
This should illistrate what I am trying to do: template <class T> T SomeFunction( T parm ) { return parm; } template <class T> class SomeClass
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
3
by: Al Grant | last post by:
Consider two translation units, (1): namespace { struct S { }; } struct D: S { virtual int f(void); }; and (2): #include <typeinfo> struct D; char const *f(D *p) { return...
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:
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...
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.