home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!haven.umd.edu!wam.umd.edu!krc
- From: krc@wam.umd.edu (Kevin R. Coombes)
- Subject: Re: new array w/ initializers...
- Message-ID: <1992Sep11.130457.10799@wam.umd.edu>
- Sender: usenet@wam.umd.edu (USENET News system)
- Nntp-Posting-Host: wam.umd.edu
- Organization: University of Maryland, College Park
- References: <16945@ucdavis.ucdavis.edu>
- Date: Fri, 11 Sep 1992 13:04:57 GMT
- Lines: 32
-
- In article <16945@ucdavis.ucdavis.edu> wagner@ike.ucdavis.edu writes:
- >class Vector {
- > private:
- > double *vals;
- > int dim;
- > public:
- > Vector (int dimension) {
- > etc...
- > }
- > ...
- >}; // End Class Vector
- >
- >main () {
- > Vector *vecs;
- > vecs = new Vector[10] (3);
- >}
- >
- >The intent is to create an array of Vectors of dimension 3.
- >I have run into this problem over and over again, and I don't
- >see any logical reason for this not being possible (except
- >possibly oversight). Surely others have had this same idea.
-
- Yes; and the solution is templates. The point to keep in mind for this
- example is that the obvious operations on vectors (vector addition, in
- particular) don't make sense between vectors of different lengths.
- The natural mathematical type of a vector contains its length as part
- of its specification -- as part of the type, not as a parameter to
- the constructor. I'm sure that someone can (and will) followup with
- an example where you really need to pass parameters to constructors
- in an array, but I don't believe that vectors are such an example.
-
- Kevin Coombes <krc@math.umd.edu>
-