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

Home Posts Topics Members FAQ

Member data in class derived from template

Hello,

The code below compiles fine on VS 6.0 and .NET 2005, but G++ complains
about mData not having been declared in the Derived class:

template<typename T>
class Base
{
public:
Base() {}
virtual ~Base() {}

protected:
int mData;
};
template<typename T>
class Derived : public Base<T>
{
public:
Derived() {}
virtual ~Derived() {}

void f()
{
mData = 0;
}
};
I have done extensive searches on the problem, including books etc., but I
have found nothing on the topic. So I am beginning to feel that I might be
fundamentally confusing something...

But then again, a Derived<T> should be a Base<T>, and those should come
with an mData member. So what could it be?

Thanks,
--
Matthias

Apr 30 '06 #1
4 1667
Matthias von Faber wrote:

But then again, a Derived<T> should be a Base<T>, and those should come
with an mData member. So what could it be?


But Base<int>, for example, can be specialized, and it isn't required to
have a member named mData. So you have to tell the compiler where mData
comes from. Instead of the unadorned

mData = 0;

you can say

this->mData = 0;
Base<T>::mData = 0;

and I think there's a third one, that isn't popping into my head at the
moment.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 30 '06 #2
Matthias von Faber wrote:
Hello,

The code below compiles fine on VS 6.0 and .NET 2005, but G++ complains
about mData not having been declared in the Derived class:

template<typename T>
class Base
{
public:
Base() {}
virtual ~Base() {}

protected:
int mData;
};
template<typename T>
class Derived : public Base<T>
{
public:
Derived() {}
virtual ~Derived() {}

void f()
{
mData = 0;
}
};
I have done extensive searches on the problem, including books etc., but I
have found nothing on the topic. So I am beginning to feel that I might be
fundamentally confusing something...

But then again, a Derived<T> should be a Base<T>, and those should come
with an mData member. So what could it be?


Two-phase name loopkup. The name has to be a dependant name. Since the Base
template is not instantiated at the point of Derived's definition, the
compiler doesn't know yet that mData is supposed to be a member of the
object.
Try:

void f()
{
this->mData = 0;
}
Apr 30 '06 #3
* Pete Becker:
Matthias von Faber wrote:

But then again, a Derived<T> should be a Base<T>, and those should come
with an mData member. So what could it be?


But Base<int>, for example, can be specialized, and it isn't required to
have a member named mData. So you have to tell the compiler where mData
comes from. Instead of the unadorned

mData = 0;

you can say

this->mData = 0;
Base<T>::mData = 0;

and I think there's a third one, that isn't popping into my head at the
moment.


You're probably thinking of a using-declaration.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 30 '06 #4
> this->mData = 0;

That fixed it nicely. Well, seems that I should read up on class templates
then. My conception was that all Base specializations would have an mData.

Thanks very much!
--
Matthias
Apr 30 '06 #5

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

Similar topics

11
4572
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member...
6
5862
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
9
1641
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
5
2951
by: dj | last post by:
I somehow understand why member function templates cannot be virtual, but my question is how I could achieve something similar to this: class A { public: template<typename Tvirtual f() = 0; } class B : public A { public:
18
2842
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything...
1
3906
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
14
4167
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
1770
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng #include <iostream>
5
3762
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static void UseMap (const std::string &key, int value)
0
7270
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...
0
7178
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...
0
7397
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7125
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...
0
7543
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...
1
5102
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3252
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...
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
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...

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.