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>:erived() { 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
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>:erived() { 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