home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18882 < prev    next >
Encoding:
Text File  |  1993-01-09  |  2.5 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Passing 2-d arrys to functions
  5. Message-ID: <1993Jan9.161019.7362@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <MATT.93Jan8111148@physics2.berkeley.edu>
  10. Date: Sat, 9 Jan 1993 16:10:19 GMT
  11. Lines: 59
  12.  
  13. In article <MATT.93Jan8111148@physics2.berkeley.edu> matt@physics.berkeley.edu writes:
  14. >In article <726521188snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
  15. >
  16. >I happen to be of this opinion.  I believe that a well-designed array
  17. >class is essential, and should be part of the C++ Standard Library.
  18. >(Actually, make that several well-designed array classes; probably the
  19. >right solution is to have one class for one-dimensional arrays, and
  20. >another for multi-dimensional arrays.)  Now that templates are part of
  21. >the language, this is more practical than it used to be.
  22.  
  23.     Not that simple! Even for 1-dimensional arrays,
  24. several classes might be needed.
  25.  
  26. >
  27. >I've written my own array classes (I imagine that we all have), but it
  28. >would be nice if I could count on arrays being there in every
  29. >implementation.
  30.  
  31.     What we need standardised most is an ABSTRACT array class.
  32.  
  33.     template<class T>
  34.     class Array<T> {
  35.     public:
  36.         virtual T& operator[](int)=0;
  37.         virtual const T& operator[](int)const =0;
  38.         virtual int count()const=0;
  39.         int operator+()const{return count();}
  40.         virtual ~Array(){}
  41.     };
  42.  
  43. Thats mine. A 'simulation' of unsafe C like arrays:
  44.  
  45.     template<class T, int len>
  46.     class CArray : public virtual  Array<T> {
  47.         T data[len];
  48.         virtual void check(int)const{}
  49.     public:
  50.         T& operator[](int i){check(i); return data[i];}
  51.         const T& operator[](int i)const {check(i); return data[i];}
  52.         int count()const{return len;}
  53.     };
  54.  
  55. Note the rare private virtual function 'check'.
  56. And a safe one:
  57.  
  58.     template<class T, int len>
  59.     class CheckedCArray : public virtual CArray<T,len> {
  60.         void check(int i)const{assert(i<count());}
  61.     };
  62.  
  63. Now do it all over again, but use a pointer to a 'new'ed
  64. array instead of 'data'. (In preparation for
  65. deriving arrays of variable length)
  66.  
  67. -- 
  68. ;----------------------------------------------------------------------
  69.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  70.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  71. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  72.