home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13528 < prev    next >
Encoding:
Text File  |  1992-09-11  |  1.5 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!wam.umd.edu!krc
  3. From: krc@wam.umd.edu (Kevin R. Coombes)
  4. Subject: Re: new array w/ initializers...
  5. Message-ID: <1992Sep11.130457.10799@wam.umd.edu>
  6. Sender: usenet@wam.umd.edu (USENET News system)
  7. Nntp-Posting-Host: wam.umd.edu
  8. Organization: University of Maryland, College Park
  9. References: <16945@ucdavis.ucdavis.edu>
  10. Date: Fri, 11 Sep 1992 13:04:57 GMT
  11. Lines: 32
  12.  
  13. In article <16945@ucdavis.ucdavis.edu> wagner@ike.ucdavis.edu writes:
  14. >class Vector {
  15. >    private:
  16. >        double *vals;
  17. >        int dim;
  18. >    public:
  19. >        Vector (int dimension) {
  20. >            etc...
  21. >        }
  22. >    ...
  23. >};  //  End Class Vector
  24. >
  25. >main () {
  26. >    Vector *vecs;
  27. >    vecs = new Vector[10] (3);
  28. >}
  29. >
  30. >The intent is to create an array of Vectors of dimension 3.
  31. >I have run into this problem over and over again, and I don't
  32. >see any logical reason for this not being possible (except
  33. >possibly oversight).  Surely others have had this same idea.
  34.  
  35. Yes; and the solution is templates. The point to keep in mind for this
  36. example is that the obvious operations on vectors (vector addition, in
  37. particular) don't make sense between vectors of different lengths.
  38. The natural mathematical type of a vector contains its length as part
  39. of its specification -- as part of the type, not as a parameter to
  40. the constructor. I'm sure that someone can (and will) followup with
  41. an example where you really need to pass parameters to constructors
  42. in an array, but I don't believe that vectors are such an example.
  43.  
  44. Kevin Coombes <krc@math.umd.edu>
  45.