home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!fmrco!pandrews!pandrews
- From: pandrews@lovat.fmrco.com (Paul Andrews)
- Subject: Re: operator [][]
- Message-ID: <1992Nov9.095352.19114@fmrco.uucp>
- Sender: news@fmrco.uucp
- Reply-To: pandrews@lovat.fmrco.com
- Organization: Fidelity Investments Ltd.
- References: <BxFt8r.2Kt@cs.columbia.edu>
- Date: Mon, 9 Nov 1992 09:53:52 GMT
- Lines: 40
-
- In article 2Kt@cs.columbia.edu, duanyang@cs.columbia.edu (Duanyang Guo) 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);
- >
- > which g++ complains about it.
- >
- > any idea or just sth is wrong with my try?
- >
- 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
-