home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!mcsun!sunic!seunet!sysinno!usoderberg
- From: usoderberg@sysinno.se (Ulf Soderberg)
- Newsgroups: comp.lang.c++
- Subject: operator [][]
- Message-ID: <721334957snx@sysinno.se>
- Date: 9 Nov 92 18:49:17 GMT
- References: <BxFt8r.2Kt@cs.columbia.edu>
- Reply-To: usoderberg@sysinno.se
- Organization: System Innovation AB
- Lines: 62
-
-
- In article <BxFt8r.2Kt@cs.columbia.edu> duanyang@cs.columbia.edu 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?
- >
- >
- > --
- > Duanyang Guo | (212)939-7098(O), 663-8904(H)
- > CS Dept, Columbia Univ. | Rm 625, CEPSR
- > New York, NY 10027 | Email:duanyang@cs.columbia.edu
- >
- >
-
- First define a vector of int's, then define a vector of vectors of int's.
-
- Ex.
- class Ivect {
- int *vec;
-
- public:
- Ivec(int n) { vec = new int[n]; }
- int &operator[](int idx) { return vec[idx]; }
- };
-
- class Imat {
- Ivec **mat;
-
- public:
- Imat(int m, int n)
- {
- mat = new (Ivec *)[m];
- for (int i = 0; i < m; i++)
- mat[i] = new Ivec(n);
- }
-
- Ivec &operator[](int idx) { return *mat[idx]; }
- };
-
- Then I think it is possible to do like this:
-
- Imat m(4, 3);
-
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 3; j++)
- m[i][j] = 0;
-
- -----------------------------------------------------------------------------
- Ulf Soderberg Telephone: +46 8 6300630
- usoderberg@sysinno.se Fax: +46 8 6300639
- Address: System Innovation AB
- Polygonvagen 53-55
- PO.Box 7105
- S183 07 Taby
- SWEDEN
-