home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: Passing 2-d arrys to functions
- Message-ID: <1993Jan9.161019.7362@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <24568@alice.att.com> <726521188snx@trmphrst.demon.co.uk> <MATT.93Jan8111148@physics2.berkeley.edu>
- Date: Sat, 9 Jan 1993 16:10:19 GMT
- Lines: 59
-
- In article <MATT.93Jan8111148@physics2.berkeley.edu> matt@physics.berkeley.edu writes:
- >In article <726521188snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk (Nikki Locke) writes:
- >
- >I happen to be of this opinion. I believe that a well-designed array
- >class is essential, and should be part of the C++ Standard Library.
- >(Actually, make that several well-designed array classes; probably the
- >right solution is to have one class for one-dimensional arrays, and
- >another for multi-dimensional arrays.) Now that templates are part of
- >the language, this is more practical than it used to be.
-
- Not that simple! Even for 1-dimensional arrays,
- several classes might be needed.
-
- >
- >I've written my own array classes (I imagine that we all have), but it
- >would be nice if I could count on arrays being there in every
- >implementation.
-
- What we need standardised most is an ABSTRACT array class.
-
- template<class T>
- class Array<T> {
- public:
- virtual T& operator[](int)=0;
- virtual const T& operator[](int)const =0;
- virtual int count()const=0;
- int operator+()const{return count();}
- virtual ~Array(){}
- };
-
- Thats mine. A 'simulation' of unsafe C like arrays:
-
- template<class T, int len>
- class CArray : public virtual Array<T> {
- T data[len];
- virtual void check(int)const{}
- public:
- T& operator[](int i){check(i); return data[i];}
- const T& operator[](int i)const {check(i); return data[i];}
- int count()const{return len;}
- };
-
- Note the rare private virtual function 'check'.
- And a safe one:
-
- template<class T, int len>
- class CheckedCArray : public virtual CArray<T,len> {
- void check(int i)const{assert(i<count());}
- };
-
- Now do it all over again, but use a pointer to a 'new'ed
- array instead of 'data'. (In preparation for
- deriving arrays of variable length)
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-