473,796 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inherited members of templated classes

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 (Base), and a
template derived class (Derived, which is publicly inherited from Base
with the same template parameters), why must I prefix all "Base"
member accesses in "Derived" with Base<template arguments>? Is there
perhaps another method that I'm not seeing. An example is shown below
template <typename T>
class Base
{
protected:
T blah;
public:
Base();
void init();
};
template <typename T>
class Derived : public Base<T>
{
public:
Derived();
void init2();
};
template <typename TBase<T>::Base( ) { init(); }
template <typename Tvoid Base<T>::init() { blah = (T)0; }
template <typename TDerived<T>::De rived() { init(); init2(); } // g+
+ raises an error about init having template arguments so a
declaration must exist
template <typename Tvoid Derived<T>::ini t2() { Base<T>::blah =
(T)0; } // why do I have to refer to this as Base<T>::blah? Why can't
I simply refer to it as blah?

Thanks,
-Jeff

Feb 7 '07 #1
1 1495
cp*****@gmail.c om wrote:
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 (Base), and a
template derived class (Derived, which is publicly inherited from Base
with the same template parameters), why must I prefix all "Base"
member accesses in "Derived" with Base<template arguments>? Is there
perhaps another method that I'm not seeing. An example is shown below
template <typename T>
class Base
{
protected:
T blah;
public:
Base();
void init();
};
template <typename T>
class Derived : public Base<T>
{
public:
Derived();
void init2();
};
template <typename TBase<T>::Base( ) { init(); }
template <typename Tvoid Base<T>::init() { blah = (T)0; }
template <typename TDerived<T>::De rived() { init(); init2(); } // g+
+ raises an error about init having template arguments so a
declaration must exist
template <typename Tvoid Derived<T>::ini t2() { Base<T>::blah =
(T)0; } // why do I have to refer to this as Base<T>::blah? Why can't
I simply refer to it as blah?
http://www.parashift.com/c++-faq-lit...html#faq-35.19

Regards,
Sumit.
Feb 7 '07 #2

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

Similar topics

3
1291
by: Dave | last post by:
Hello all, Are the rules for templated member functions different in any way than the rules for templated non-member functions? How about templated member classes Vs. templated non-member classes? Thanks, Dave
4
1869
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
4
1636
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a handful of inherited classes. The static method calls the ctor of the appropriate inherited class and returns it as type IBaseClass. There are no new methods or properties in the inherited classes and public clients never need to know which...
6
1771
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
43
479
by: lovecreatesbeauty | last post by:
Perhaps all C++ programmers are busy in designing their classes or get into a deep sleep after a through night hard work. So I am willing to ask the "off-topic" (perhaps not) question in this group. I think you understand both C and C++ entirely, if not, why you still work on the C programming language. And you are experts on both them, so it is not off-topic at all. I need your help. Please let us go into the topic. I'm disturbed on...
2
2828
by: Amadeus W. M. | last post by:
Template member functions cannot be virtual - I know - but is there any way to simulate that (other than making the entire class templated, with non-templated members). What I have is this: class Transform { public:
14
2645
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
21
2220
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not necessarily the same... class base { int x;
19
2236
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
0
9680
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
10456
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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
10174
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,...
1
7548
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.