home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!decwrl!pacbell.com!network.ucsd.edu!sdcc12!cs!pluto
- From: pluto@cs.ucsd.edu (Mark Plutowksi)
- Newsgroups: comp.lang.c++
- Subject: Re: operator [][]
- Message-ID: <40779@sdcc12.ucsd.edu>
- Date: 9 Nov 92 19:03:38 GMT
- References: <BxFt8r.2Kt@cs.columbia.edu> <1992Nov9.095352.19114@fmrco.uucp>
- Sender: news@sdcc12.ucsd.edu
- Lines: 92
- Nntp-Posting-Host: beowulf.ucsd.edu
-
- pandrews@lovat.fmrco.com (Paul Andrews) writes:
-
-
- >> for multiple dimension array, is it possible to define
- >> an operator [][](say for two dimensions)? I try to define
- >> it as
- >> int & operator[][] (int, int);
- >>
- >You can only overload single []. Remember though that this generates a function call
- >like:
- > <class>.operator[](<arg>)
-
- >which returns a value. So make that value be a reference to another class which also
- >has an operator[] overload. Then when you specify:
-
- > <classA>[<arg1>][<arg2>]
-
- >you will actually get
-
- > <classA>.operator[](<arg1>).<classB>.operator[](<arg2>)
-
- >classB should now call a well-known function in classA to indicate what argument was
- >passed to it. A good way of doing this is for classA to inherit yet another class
- >(classC!) which has a well-known virtual function which classA overrides. That way
- >both classC and classB can be re-used whenever you want to have multiple [].
-
- >Simple huh?
-
- >(apologies if the syntax of the expanded function calls isn't quite correct).
-
- >(I would be interested to know if anyone has any other ways of doing this).
-
- >(Is there any standard nomenclature for classes like C and B above? something like
- >classB is a slave class, classC is a functor or what?)
-
- >---
- >---
- >Paul Andrews
-
-
- I think that what I did is another way:
- I have a Matrix class that contains a data member which is
- an array of pointers to Vectors, where Vector is another class, whose
- primary data member is an array of pointers to some number type (double, int, ...)
- Let aVec be a vector. Then, aVec[i] gives its i-th element, (because I specified
- the [] operator as above.) So, I just give the Matrix class the []
- operator as well, and voila, aMatrix[][] gives me what I want -
- which I gather is also what you're trying to do.
- It does, however, distribute the internal storage of your array
- (whereas as I understand it the above solution retains the array
- as a single object referenced by two classes using two access functions)
- In my case, this was desirable, because splitting up large arrays
- into small Vectors grouped into a Matrix allowed me to use arrays much
- larger than the 64K segments my machine constrains me to.
-
- .
- .
- .
- .
- .
- .
- .
- .
- i
-
- have
-
- no
-
- signature
-
-
- please
-
-
- let
-
-
- this
-
-
- suffice
-
- Mr.
-
- Auto
-
- reject
-
- Program
-
- Sir
-