home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky gnu.g++.help:1593 comp.lang.c++:17995
- Newsgroups: uw.dcs.general,gnu.g++.help,comp.lang.c++
- Path: sparky!uunet!stanford.edu!agate!doc.ic.ac.uk!warwick!dcs.warwick.ac.uk!maths.warwick.ac.uk!steve
- From: steve@maths.warwick.ac.uk (Steve Rumsby)
- Subject: Re: Deriving C++ Templates
- Message-ID: <1992Dec15.162041.26467@dcs.warwick.ac.uk>
- Sender: news@dcs.warwick.ac.uk (Network News)
- Nntp-Posting-Host: avon
- Organization: Geometry Group, Maths Institute, Warwick University, UK.
- References: <1992Dec15.160544.26065@dcs.warwick.ac.uk>
- Date: Tue, 15 Dec 1992 16:20:41 GMT
- Lines: 41
-
- 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
-