473,625 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ 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>::myfun c()':
temptest1.cpp:1 0: error: `base_member' was not declared in this scope

However changing the myfunc() line to

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

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 1584

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>::myfun c()':
temptest1.cpp:1 0: error: `base_member' was not declared in this scope

However changing the myfunc() line to

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

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
6569
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? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
2
5674
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 seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
4
1859
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
11
1919
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 <typename T> virtual void setParam(std::string s, const T& value);
17
2208
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 map<string, Twhere T is template argument) To hold the value type I have something like: template<class T>
3
1556
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. This does not seem to work. Below is the example I used and some of the things I tried and the gnu errors I got. There are only two classes, Base and Derived, where Base defines the buffer, and Derived tries to set a pointer to it. Is this...
4
3134
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
2923
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
3428
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 typeid(p).name(); }
0
8259
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8192
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4090
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.