Jeff Abrahamson on Sun, 16 Feb 2003 20:11:10 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] C++: deriving from templated classes


On Sun, Feb 16, 2003 at 07:17:42PM -0500, Jeff Abrahamson wrote:
> Looking for more syntax help, I've gotta get a copy of version 3 of
> Stroustrup soon.
> 
> I'm trying to derive from a templated class, but I'm not getting the
> syntax right. The C++ faq and google aren't helping me here. Pointers
> or outright answers appreciated.
> 
> Thanks much. Here's the simple example:
> 
> 
>     template <class T>
>     class SwapArray {
> 
>     };
> 
> 
>     class Bar : public template <class T> SwapArray {
>     //class Bar : public SwapArray {
> 
>     };

The answer, I think, is two fold:

First, since Bar makes specific the class T, it has to specify it:

    class Bar : public SwapArray<Foo> { ... };

Second, forward declarations of template classes aren't good enough:
the entire class definition must precede it, even though this means an
include of a .h in another .h.

Indeed, the implementation of a template class has to go with the
template classes declaration, near as I can tell, since the compiler
needs to be able to generate the appropriate code at compilation time
for the code that uses the template class.

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

Attachment: pgpDj1ir8eLJa.pgp
Description: PGP signature