🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Jason Morris Tue, 15 Dec 1992 16:05
Deriving C++ Templates
#4
Author: Jason Morris
Date: Tue, 15 Dec 1992 16:05
11 lines
1020 bytes
  I have a C++ class, Matrix, and from this I derived a class, Vector, which
is simply a 1-D Matrix. The problem was that I could only have a Matrix of,
say floats, but I wanted something more flexible that could hold anything. So
I changed Matrix to be a template with the type of element given as the
template arguement. That works fine.
  However, I am now having problems in deriving Vector. Is it actually
possible to use a template as a base class? Or are parameterised types and
inheritance mechanisms mutually exlusive? 

    Jas.
Re: Deriving C++ Templates
#5
Author: Steve Rumsby
Date: Tue, 15 Dec 1992 16:20
41 lines
1934 bytes
In article <1992Dec15.160544.26065@dcs.warwick.ac.uk>, jas@dcs.warwick.ac.uk (Jason Morris) writes:
> 
>   I have a C++ class, Matrix, and from this I derived a class, Vector, which
> is simply a 1-D Matrix. The problem was that I could only have a Matrix of,
> say floats, but I wanted something more flexible that could hold anything. So
> I changed Matrix to be a template with the type of element given as the
> template arguement. That works fine.
>   However, I am now having problems in deriving Vector. Is it actually
> possible to use a template as a base class? Or are parameterised types and
> inheritance mechanisms mutually exlusive? 
>
Remember that a class template defines a family of classes, and is not a
class itself. It is therefore not possible to use the template as a base class
(since it is not a class) but it is possible to use members of the defined
family as base classes. So, given:

	template <class T>
	class foo {
		// junk...
	};

You can say:

	class foobar : private foo<int> {
		// junk...
	};

You can also say:

	template <class T>
	class foobaz : private foo<T> {
		// junk...
	};

I think this final example does what you want - you derive Vector<T> from
Matrix<T>.

Steve.
-- 
UUCP:	 ...!uknet!warwick!steve	Internet: steve@maths.warwick.ac.uk
JANET:	 steve@uk.ac.warwick.maths	PHONE:	 +44 203 524657
Re: Deriving C++ Templates
#6
Author: John MAX Skaller
Date: Wed, 16 Dec 1992 14:23
16 lines
1243 bytes
In article <1992Dec15.160544.26065@dcs.warwick.ac.uk> jas@dcs.warwick.ac.uk (Jason Morris) writes:
>
>  However, I am now having problems in deriving Vector. Is it actually
>possible to use a template as a base class? Or are parameterised types and
>inheritance mechanisms mutually exlusive? 
>
	It is definitely the intent this be allowed, BC allows it.
So do most of the more recent versions of other compilers. Some older
compilers may have had problems.


-- 
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
	Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
Re: Deriving C++ Templates
#7
Author: Maz H. Spork
Date: Wed, 16 Dec 1992 15:01
16 lines
835 bytes
You'll have to explicitly state in the derived class which type of the
base class you want to use, ie:

   template <class C>
      class Base<T> { };

   template <class C>
      class Derived<C> : Base <C> { };

or similar, or simpler, like fewer template dependencies.

By the way, for good programming practice, consider deriving Matrix from
Vector rather than the other way round...


Maz
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads