home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / gnu / g / help / 1593 < prev    next >
Encoding:
Text File  |  1992-12-15  |  1.9 KB  |  55 lines

  1. Xref: sparky gnu.g++.help:1593 comp.lang.c++:17995
  2. Newsgroups: uw.dcs.general,gnu.g++.help,comp.lang.c++
  3. Path: sparky!uunet!stanford.edu!agate!doc.ic.ac.uk!warwick!dcs.warwick.ac.uk!maths.warwick.ac.uk!steve
  4. From: steve@maths.warwick.ac.uk (Steve Rumsby)
  5. Subject: Re: Deriving C++ Templates
  6. Message-ID: <1992Dec15.162041.26467@dcs.warwick.ac.uk>
  7. Sender: news@dcs.warwick.ac.uk (Network News)
  8. Nntp-Posting-Host: avon
  9. Organization: Geometry Group, Maths Institute, Warwick University, UK.
  10. References:  <1992Dec15.160544.26065@dcs.warwick.ac.uk>
  11. Date: Tue, 15 Dec 1992 16:20:41 GMT
  12. Lines: 41
  13.  
  14. In article <1992Dec15.160544.26065@dcs.warwick.ac.uk>, jas@dcs.warwick.ac.uk (Jason Morris) writes:
  15. >   I have a C++ class, Matrix, and from this I derived a class, Vector, which
  16. > is simply a 1-D Matrix. The problem was that I could only have a Matrix of,
  17. > say floats, but I wanted something more flexible that could hold anything. So
  18. > I changed Matrix to be a template with the type of element given as the
  19. > template arguement. That works fine.
  20. >   However, I am now having problems in deriving Vector. Is it actually
  21. > possible to use a template as a base class? Or are parameterised types and
  22. > inheritance mechanisms mutually exlusive? 
  23. >
  24. Remember that a class template defines a family of classes, and is not a
  25. class itself. It is therefore not possible to use the template as a base class
  26. (since it is not a class) but it is possible to use members of the defined
  27. family as base classes. So, given:
  28.  
  29.     template <class T>
  30.     class foo {
  31.         // junk...
  32.     };
  33.  
  34. You can say:
  35.  
  36.     class foobar : private foo<int> {
  37.         // junk...
  38.     };
  39.  
  40. You can also say:
  41.  
  42.     template <class T>
  43.     class foobaz : private foo<T> {
  44.         // junk...
  45.     };
  46.  
  47. I think this final example does what you want - you derive Vector<T> from
  48. Matrix<T>.
  49.  
  50. Steve.
  51. -- 
  52. UUCP:     ...!uknet!warwick!steve    Internet: steve@maths.warwick.ac.uk
  53. JANET:     steve@uk.ac.warwick.maths    PHONE:     +44 203 524657
  54.