Inherited members of templated classes

C

cpunerd

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 T> Base<T>::Base() { init(); }
template <typename T> void Base<T>::init() { blah = (T)0; }
template <typename T> Derived<T>::Derived() { init(); init2(); } // g+
+ raises an error about init having template arguments so a
declaration must exist
template <typename T> void Derived<T>::init2() { 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
 
S

Sumit Rajan

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 T> Base<T>::Base() { init(); }
template <typename T> void Base<T>::init() { blah = (T)0; }
template <typename T> Derived<T>::Derived() { init(); init2(); } // g+
+ raises an error about init having template arguments so a
declaration must exist
template <typename T> void Derived<T>::init2() { 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-lite/templates.html#faq-35.19

Regards,
Sumit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top