Topic: Overload [][] operator


Author: bhurt@blue.weeg.uiowa.edu (B. Hurt)
Date: 25 Nov 1994 22:39:32 -0600
Raw View
wilkinso@gdls.com (Robert M. Wilkinson) writes:

>In article <3adv52$6jtp@blue.weeg.uiowa.edu>,
>B. Hurt <bhurt@blue.weeg.uiowa.edu> wrote:
>>
>>Of course there is a much wider question involved here- why stop at
>>operator [][]?  Why not operator *+?  (It'd make vector users really
>>happy- think of the performance gains!).  etc..

>And don't forget about operator[][][], operator[][][][], and
>operator[][][][][][][][][][][][][][][][][][][][][][][][][][][].

Aw- I want an operator []*+/=!=%(),! (void).

Obfuscated C++, anyone?

>--
>____________________________________________________________________________
>
>Rob Wilkinson                                              wilkinso@gdls.com
>____________________________________________________________________________





Author: bhurt@blue.weeg.uiowa.edu (B. Hurt)
Date: 18 Nov 1994 12:29:31 -0600
Raw View
tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich) writes:

>John Barton (jjb@watson.ibm.com) wrote:


> I think the standart should interpret [i,j] as [i][j], considering that
>both arrays are of the same type.  It would be more intuitive.

No no no no no no no!

The standard classic C way of multiply dereferncing multidimensional arrays
is a[i][j], not a[i, j].  As a different thread has already pointed out,
the second already has a defined meaning, to wit a[i, j] == (i, a[j]),
which (if i has no side effects) == a[j].  (I beleive the thread is
"Why no operator[] (T, T)?").

Let's try to at least to keep it looking like C.

Wether or not there should be allowed an operator[][] (T, T)is a
different arguent all together.

>--
>-----------------------------------
>| Thomas Armstrong Kobrich        |
>| tarmstro@caleuche.dis.udec.cl   |
>| Concepcion - Chile              |
>-----------------------------------

bhurt@blue.weeg.uiowa.edu
"Old programmers never die, they are just executed in batches."




Author: jjb@watson.ibm.com (John Barton)
Date: Sun, 20 Nov 1994 15:24:35 GMT
Raw View
In article <3airqb$2tgh@blue.weeg.uiowa.edu>, bhurt@blue.weeg.uiowa.edu (B. Hurt) writes:
|> tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich) writes:
|>
|> >John Barton (jjb@watson.ibm.com) wrote:
|>
|>
|> > I think the standart should interpret [i,j] as [i][j], considering that
|> >both arrays are of the same type.  It would be more intuitive.
|>
[ comments on misquote above deleted ]

     John Barton (jjb@watson.ibm.com) did NOT write that and I DON'T
think the standard should be changed in this way.

     I do believe that we should collectively sneer at any use of the
comma operator in any code that can be changed.  If the comma operator
vanished, [i,j] could become the syntax error it deserves to be.

     I did write that multidimensional array classes need both subscripting
and projection operations; operator[](T) works just fine for projections
and operator()(T,T,T) works just fine for subscripting.


--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598




Author: wilkinso@gdls.com (Robert M. Wilkinson)
Date: 23 Nov 1994 07:19:50 -0500
Raw View
In article <3adv52$6jtp@blue.weeg.uiowa.edu>,
B. Hurt <bhurt@blue.weeg.uiowa.edu> wrote:
>
>Of course there is a much wider question involved here- why stop at
>operator [][]?  Why not operator *+?  (It'd make vector users really
>happy- think of the performance gains!).  etc..

And don't forget about operator[][][], operator[][][][], and
operator[][][][][][][][][][][][][][][][][][][][][][][][][][][].

--
____________________________________________________________________________

Rob Wilkinson                                              wilkinso@gdls.com
____________________________________________________________________________





Author: bhurt@blue.weeg.uiowa.edu (B. Hurt)
Date: 16 Nov 1994 15:55:46 -0600
Raw View
jjb@watson.ibm.com (John Barton) writes:

>In article <Cz2Lox.KGL@world.std.com>, tob@world.std.com (Tom O Breton) writes:
>|> warwick@cs.uq.oz.au (Warwick Allison) writes:
>|> > A cheaper way is to use the () operator:
>|>
>|> Yes. I understood the question as how to do it and retain the [] syntax.
>|>
>|>         Tom
>|>
>|> --
>|> finger me for how Tehomega is coming along (at tob@world.std.com)
>|> Author of The Burning Tower (from TomBreton@delphi.com) (weekly in
>|> rec.games.frp.archives)
>|>

>   Most often this question arises in the discussion of multi-dimensional
>arrays.  Two kinds of access to elements of arrays are commonly used:
>random access to individual elements and cascaded projections (matrix,
>vector, element).  For C++ built-in arrays (C-style), only cascaded projection
>is allowed.  Thus a[1] is always an array of dimension one less than
>the dimension of a, and a[1][2] is always double projection.  The C++
>operator[] was designed to support language extensions that "look-like"
>built-in language: operator[] exists to support projection, not subscripting.

Technically, yes.  In C, a[i] == *(a + i), so a[i][j] == *(*(a + i) + j), so
a is a pointer to an array of pointers (or at least acts like it).  Few
compilers (and, I would venture, no good ones) actually do it this way-
a[i][j] is translated to be *(a + (i * cols) + j), saving both unnessecary
overhead and possibly cpu cycles (The biggest savings are still in reduced
memory).  Similar reasons obtain in C++ - the "classic" way of doing a
matrix in C++ is something like:
   class vector {
      float& operator [] (int n);
    };

   class matrix {
      vector& operator[] (int n);
    };
The problem is that a construct like:
   matrix a;
   a[1][2] = 3.0;
requires two (possibly inline) function calls and the probable construction
of a temporary (of class vector).

Big Deal, you say.  the Operator []'s should be inline anyways, and with
small, fast constructors, creating temporaries is not all that painful,
and a good optimizing compiler should produce code that is comprably as
fast as the original C MD-Array dereferncing.  At least, in this case.
It is possible to contrust examples wherein the compiler is forced to do
the worst-case implementation (two function calls & a temporary), but a
programmer-defined implementation would not.  The option should at least
be there.

>  The only way to use operator[] for subscripting is to design a self-similar
>array type such that the projection of the array has the same type as the
>array.  This is very difficult; it is achieved in built-in C++ by making
>all arrays in to pointers: a pointer is a pointer, projections return pointer,
>etc.  Almost all real array classes need **BOTH** projection and subscripting:
>use operator[] for projection and operator() for subscripting.
This breaks the "C feel" of the code.  At least at this level, C++ should
have the look & feel of C.  operator () doesn't do this.

>  Since C++ does not limit user arrays to be self-similar, the ANSI committee
>could do us all a great big favor by changing the name of the operator[]
>from "subscripting" to "projection" operator.  With one vote and one sed
>script we could be rid of this question forever.

Of course there is a much wider question involved here- why stop at
operator [][]?  Why not operator *+?  (It'd make vector users really
happy- think of the performance gains!).  etc..

The only problem I can think of is recgnition- how does the compiler
know it can use the "optimized multi-operators"?  The simple way around
this is that to gaurentee that the multi-operators will not be used in
incorrect circumstances (this needs to be said _much_ better, but you
get the idea).  Note- it is not gauenteed the multi-operator will
_ever_ be used.  If it is, well and good.  If not, the programmer
should supply the "normal" way of doing things (how ever many function
calls and temporaries this requires).

>--
>John.

>John J. Barton        jjb@watson.ibm.com            (914)784-6645
>H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598




Author: tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich)
Date: Thu, 17 Nov 1994 15:36:52 GMT
Raw View
John Barton (jjb@watson.ibm.com) wrote:

>   The only way to use operator[] for subscripting is to design a self-similar
> array type such that the projection of the array has the same type as the
> array.  This is very difficult; it is achieved in built-in C++ by making
> all arrays in to pointers: a pointer is a pointer, projections return pointer,
> etc.  Almost all real array classes need **BOTH** projection and subscripting:
> use operator[] for projection and operator() for subscripting.

>   Since C++ does not limit user arrays to be self-similar, the ANSI committee
> could do us all a great big favor by changing the name of the operator[]
> from "subscripting" to "projection" operator.  With one vote and one sed
> script we could be rid of this question forever.

> John J. Barton        jjb@watson.ibm.com            (914)784-6645
> H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598


 I absolutely agree with you.
 I think the standart should interpret [i,j] as [i][j], considering that
both arrays are of the same type.  It would be more intuitive.

--
-----------------------------------
| Thomas Armstrong Kobrich        |
| tarmstro@caleuche.dis.udec.cl   |
| Concepcion - Chile              |
-----------------------------------




Author: ctj@fep65.fns.com (Clifton Jones)
Date: 17 Nov 1994 14:28:38 GMT
Raw View
We use [][] in a dictionary that returns a dictionary.  The dictionary
class has an operator[] member that takes the key.  So if you want a
2-dimensional dictionary you have to do something like this.

typedef Dictionary<String,String> NestedDictionary;
Dictionary<String,NestedDictionary> myDictionary;

You can then do:

cout << myDictionary["foo"]["bar"] << endl;



--
---------------------------------------------------------------------------
Clifton Jones
Fujitsu Network Switching
email: ctj@fns.com
phone: (919) 790-3173

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6

mQCNAi4b8nQAAAEEAOUdgp9XkQbrtT0Z5aJ8c5g5CzpazhWcgodvMDJW/usZnHK7
XKe6RVGEV2BPdDeXuqqlXJeovus+4ZsydrCb5Fj/NkrSGFvGjMaShaXDPKoe5weS
kod0xnIT8Pnjl6IFi3G4D0niZj0Fjd5LaZjWUkbbj7zzgGYctaC/NmsZrQGhAAUR
tB5DbGlmdG9uIFQuIEpvbmVzIDxjdGpAZm5zLmNvbT4=
=0wiZ
-----END PGP PUBLIC KEY BLOCK-----




Author: kevlin@wslint.demon.co.uk (Kevlin Henney)
Date: Fri, 18 Nov 1994 10:13:31 +0000
Raw View
In article <1994Nov17.153652.16464@halcon.dpi.udec.cl>
           tarmstro@caleuche.dis.udec.cl "Thomas Armstrong Kobrich" writes:

[snip]
>        I absolutely agree with you.
>        I think the standart should interpret [i,j] as [i][j], considering that
>both arrays are of the same type.  It would be more intuitive.

To whom, and why would it be more intuitive? We're talking about C and C++,
not Modula-2 or Pascal. Hacking the language to appeal to such programmers
will achieve little.

+---------------------------+-------------------------------------------+
| Kevlin A P Henney         | Human vs Machine Intelligence:            |
| kevlin@wslint.demon.co.uk | Humans can wreck a nice beach more easily |
| Westinghouse Systems Ltd  |                                           |
+---------------------------+-------------------------------------------+




Author: tob@world.std.com (Tom O Breton)
Date: Thu, 10 Nov 1994 03:17:22 GMT
Raw View
bflorman@indy.net (Bruce A. Florman) writes:
> While there is no operator[][], I can't think of any reason one can't be
> simulated.  Simply have your matrix class' operator[] return an instance
> of another class whose operator[] accesses the appropriate element.  I

You are of course correct, a double array-access can be simulated by
using an intermediate class.

The thing is that it's a pretty big production when all you want for a
class to be dereferenced twice (or apply two offsets to an internal
array, or whatever the mechanism is for the class in question).

Often you can use built-in types (pointers, of course) to bring down the
overhead. This disables any form of checking except on the first
parameter, though.

        Tom

--
finger me for how Tehomega is coming along (at tob@world.std.com)
Author of The Burning Tower (from TomBreton@delphi.com) (weekly in
rec.games.frp.archives)





Author: warwick@cs.uq.oz.au (Warwick Allison)
Date: 10 Nov 1994 05:04:19 GMT
Raw View
gschwarz@dcc.uchile.cl (Guillermo Schwarz Utrup) writes:

> ...

>--
>For an experienced C programmer, expecting to use
>the "OO features" of C++ well after an intensive
>4 week long class is about like expecting to be able
>to fly an F-16 after becoming proficient with a bike.

Easy, just cast the bike to a vehicle, then cast the vehicle to an f16.
Be sure to surround the bike with plenty of dummy vehicles, as they will
be trashed by the fly method.

--
Warwick
--
  _-_|\      warwick@cs.uq.oz.au            /     Microsoft is not the answer.
 /     * <-- Computer Science Department,  /     Microsoft is the question.
 \_.-._/     University of Queensland,    /
      v      Brisbane, Australia.        /     NO is the answer.




Author: warwick@cs.uq.oz.au (Warwick Allison)
Date: 10 Nov 1994 05:06:59 GMT
Raw View
tob@world.std.com (Tom O Breton) writes:

>The thing is that it's a pretty big production when all you want for a
>class to be dereferenced twice (or apply two offsets to an internal
>array, or whatever the mechanism is for the class in question).

A cheaper way is to use the () operator:

class Matrix {
 element& operator() (int i, int j) { return data[i][j]; }
}

Then:
 Matrix foo;
 foo(1,5);

--
Warwick
--
  _-_|\      warwick@cs.uq.oz.au            /     Microsoft is not the answer.
 /     * <-- Computer Science Department,  /     Microsoft is the question.
 \_.-._/     University of Queensland,    /
      v      Brisbane, Australia.        /     NO is the answer.




Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Thu, 10 Nov 1994 16:13:45 GMT
Raw View
In article <1994Nov09.212608.23327@halcon.dpi.udec.cl>,
Thomas Armstrong Kobrich <tarmstro@caleuche.dis.udec.cl> wrote:
>Matthew Shea (mtshea@sdcc10.ucsd.edu) wrote:
>> Does any one how to overload the [][] operator??.. I don't think
>> that you can do it, so does any one know how to overload the comma
>> operator so that 2 parameters could be passed in the [] operator,
>> like [10,3] would represent [10][3].. The reason I want to do this
>
> I don't know if it works, but try:
>
>  #define ][ ,
>
> (is this legal??)

 Fortunately, no.
 -- Pete




Author: tob@world.std.com (Tom O Breton)
Date: Thu, 10 Nov 1994 21:14:56 GMT
Raw View
warwick@cs.uq.oz.au (Warwick Allison) writes:
> A cheaper way is to use the () operator:

Yes. I understood the question as how to do it and retain the [] syntax.

        Tom

--
finger me for how Tehomega is coming along (at tob@world.std.com)
Author of The Burning Tower (from TomBreton@delphi.com) (weekly in
rec.games.frp.archives)





Author: gschwarz@dcc.uchile.cl (Guillermo Schwarz Utrup)
Date: Wed, 9 Nov 1994 22:32:49 GMT
Raw View
class Vector
{
   int * v;
public:
   int& operator [] ( int i ) { return v[ i ]; }
};

class Matrix
{
    Vector * v;
public:
    Vector& operator [] ( int i ) { return v[ i ]; }
};

void main()
{
   Vector v( 4, 5 );
   v[ 3 ][ 2 ] = v[ 2 ][ 3 ] + 5;
}
--
For an experienced C programmer, expecting to use
the "OO features" of C++ well after an intensive
4 week long class is about like expecting to be able
to fly an F-16 after becoming proficient with a bike.




Author: matt@foresight.Berkeley.EDU (Matt)
Date: 11 Nov 1994 01:38:43 GMT
Raw View
[Followups redirected to comp.lang.c++, since this is no longer relevant
to the C++ standardization process.]

> > A cheaper way is to use the () operator:
>
> Yes. I understood the question as how to do it and retain the [] syntax.

True.  And, of course, the answer is that an expression like A[i][j]
requires that A[i] return an object of some helper class, and that
that class's operator[] do something appropriate.  If you declare the
appropriate member functions inline, this is no less efficient than
calling a single function A.Aref(i,j).

At some point, though, you have to ask: is that sort of trick worth
it?  It means that you have one more class in your program, and that
you have to write its constructors and destructors; it means that
accessing an element of A is a moderately complicated operation, one
that's to debug and to maintain.  (Consider what happens if you want
to change the implementation of how your data are stored.  Instead of
changing a single access function, you have to change two access
functions---your main class's and your helper class's---and you also
have to change your helper class's constructor.)

I'm beginning to think that something like this falls into the
category of "stupid C++ tricks": yes, you can do it, and it's nice
that you can mimic C's array syntax, but is it really worth it?  For
the minor gain of getting C's array access syntax, you end up writing
a class that's much more complicated than if you were just willing to
write A(i,j) or A.Get(i,j).

And it's not as if there's anything particularly wonderful about C's
array access syntax, anyway.  Lots of languages use syntax like A(i,j)
anyway; in the end, it's no more or less natural than C's syntax.
--

                               --matt




Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Thu, 10 Nov 1994 18:30:08 GMT
Raw View
In <75521@sdcc12.ucsd.edu> mtshea@sdcc10.ucsd.edu (Matthew Shea) writes:

 WRONG NEWSGROUP!!!!

>Does any one how to overload the [][] operator??..

 There is no such operator; but you haven't thought very hard
 about how [][] works for built-in matrices.  Again, there, there
 is no [][] operator either.

>I don't think that you can do it, so does any one know how to overload
>the comma operator so that 2 parameters could be passed in the []
>operator, like [10,3] would represent [10][3]..

 Not possible.  Period.
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL




Author: richardcox@cix.compulink.co.uk ("R Cox Zergo Ltd")
Date: Fri, 11 Nov 1994 17:42:47 GMT
Raw View
In <1994Nov09.212608.23327@halcon.dpi.udec.cl>
tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich) wrote:
> Matthew Shea (mtshea@sdcc10.ucsd.edu) wrote:
> > Does any one how to overload the [][] operator??.. I don't think
> > that you can do it, so does any one know how to overload the comma
> > operator so that 2 parameters could be passed in the [] operator,
> > like [10,3] would represent [10][3].. The reason I want to do this
>
>       I don't know if it works, but try:
>
>               #define ][ ,
>
>       (is this legal??)

The #define be legal, but will almost certinally break existing code.
Since every existing
        x[a][b]
will be converted into x[a,b], which will only work if [ , ] is supported
for those types. Also you would need to defined operatot,(int, int) for
the indexes which is not legal.

So #define ][ ,
is legal, but wouldn't really help!

-------------------------------------------
Richard Cox: richardcox@cix.compulink.co.uk
Zergo Limited, Innovation House, Mark Road,
Hemel Hempstead, Herts, HP2 7DN, UK




Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Fri, 11 Nov 1994 19:46:24 GMT
Raw View
In article <Cz46JB.978@cix.compulink.co.uk>,
R Cox Zergo Ltd <richardcox@cix.compulink.co.uk> wrote:
>
>In <1994Nov09.212608.23327@halcon.dpi.udec.cl>
>tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich) wrote:
>> Matthew Shea (mtshea@sdcc10.ucsd.edu) wrote:
>> > Does any one how to overload the [][] operator??.. I don't think
>> > that you can do it, so does any one know how to overload the comma
>> > operator so that 2 parameters could be passed in the [] operator,
>> > like [10,3] would represent [10][3].. The reason I want to do this
>>
>>       I don't know if it works, but try:
>>
>>               #define ][ ,
>>
>>       (is this legal??)
>
>The #define be legal, but will almost certinally break existing code.

 The #define is NOT LEGAL. In both C and C++ a #define directive must
be followed by an identifier. ][ is not an identifier.
 -- Pete




Author: bflorman@indy.net (Bruce A. Florman)
Date: Wed, 9 Nov 1994 21:52:57 GMT
Raw View
Tom O Breton (tob@world.std.com) wrote:
: mtshea@sdcc10.ucsd.edu (Matthew Shea) writes:
: > Does any one how to overload the [][] operator??.. I don't think
: > that you can do it, so does any one know how to overload the comma
: > operator so that 2 parameters could be passed in the [] operator,

: There was some discussion a few months back proposing an (or a family
: of) operator[][], but there is no such thing in C++ as it stands or as
: it is planned for the foreseeable future.


While there is no operator[][], I can't think of any reason one can't be
simulated.  Simply have your matrix class' operator[] return an instance
of another class whose operator[] accesses the appropriate element.  I
think that the code below does what was requested. Or do I not understand
the question?

--Bruce Florman

------------------------------------------------------------------------

#include <iostream.h>

template <class T>
class Matrix
{
public:

    Matrix (unsigned nRows, unsigned nColumns)
      : m_elements (new T [nRows * nColumns]),
        m_nRows    (nRows),
        m_nColumns (nColumns)
    {
        // Nothing more.
    }

    ~Matrix ()
    {
        delete m_elements;
    }

    class RowAccessor
    {
    private:
        friend Matrix<T>;
        Matrix<T> & m_matrix;
        int         m_rowIndex;
        RowAccessor (Matrix<T> & matrix, int rowIndex)
          : m_matrix   (matrix),
            m_rowIndex (rowIndex)
        {
            // Nothing more to initialize.
        }
    public:
        T & operator [] (int columnIndex)
        {
            return m_matrix.access(m_rowIndex, columnIndex);
        }
    };

    RowAccessor operator [] (int rowIndex)
    {
        return RowAccessor(*this, rowIndex);
    }

    T & access (int rowIndex, int columnIndex)
    {
        return m_elements[rowIndex * m_nColumns + columnIndex];
    }

    unsigned NumRows    () const { return m_nRows; }
    unsigned NumColumns () const { return m_nColumns; }

private:

    T      * const m_elements;
    unsigned const m_nRows;
    unsigned const m_nColumns;
};

int
main ()
{
    Matrix<char> m(3, 3);

    for (int i = 0; i < m.NumRows(); ++i)
    {
        for (int j = 0; j < m.NumColumns(); ++j)
        {
            m[i][j] = 'A' + i + j;
        }
    }

    cout << "Diagonal: "<< m[0][0] << m[1][1] << m[2][2] << endl;

    return 0;
}




Author: jjb@watson.ibm.com (John Barton)
Date: Fri, 11 Nov 1994 13:27:13 GMT
Raw View
In article <Cz2Lox.KGL@world.std.com>, tob@world.std.com (Tom O Breton) writes:
|> warwick@cs.uq.oz.au (Warwick Allison) writes:
|> > A cheaper way is to use the () operator:
|>
|> Yes. I understood the question as how to do it and retain the [] syntax.
|>
|>         Tom
|>
|> --
|> finger me for how Tehomega is coming along (at tob@world.std.com)
|> Author of The Burning Tower (from TomBreton@delphi.com) (weekly in
|> rec.games.frp.archives)
|>

   Most often this question arises in the discussion of multi-dimensional
arrays.  Two kinds of access to elements of arrays are commonly used:
random access to individual elements and cascaded projections (matrix,
vector, element).  For C++ built-in arrays (C-style), only cascaded projection
is allowed.  Thus a[1] is always an array of dimension one less than
the dimension of a, and a[1][2] is always double projection.  The C++
operator[] was designed to support language extensions that "look-like"
built-in language: operator[] exists to support projection, not subscripting.

  The only way to use operator[] for subscripting is to design a self-similar
array type such that the projection of the array has the same type as the
array.  This is very difficult; it is achieved in built-in C++ by making
all arrays in to pointers: a pointer is a pointer, projections return pointer,
etc.  Almost all real array classes need **BOTH** projection and subscripting:
use operator[] for projection and operator() for subscripting.

  Since C++ does not limit user arrays to be self-similar, the ANSI committee
could do us all a great big favor by changing the name of the operator[]
from "subscripting" to "projection" operator.  With one vote and one sed
script we could be rid of this question forever.

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598




Author: mtshea@sdcc10.ucsd.edu (Matthew Shea)
Date: 8 Nov 94 08:44:54 GMT
Raw View
Does any one how to overload the [][] operator??.. I don't think
that you can do it, so does any one know how to overload the comma
operator so that 2 parameters could be passed in the [] operator,
like [10,3] would represent [10][3].. The reason I want to do this
is that I make a two dimensional triagular array class, and I want
to be able to use it, or very similarly like, a two _d rectangular
array.  I already finished the class with an accessor function, but
I want it to be more abstract from the user.
Thanks for any help you can give me
Matt Shea
mtshea@sdcc10.ucsd.edu





Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 8 Nov 1994 09:53:12 GMT
Raw View
In article <75521@sdcc12.ucsd.edu>,
Matthew Shea <mtshea@sdcc10.ucsd.edu> wrote:
>Does any one how to overload the [][] operator??...

There is no such operator in C++.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 8 Nov 1994 12:14:09 -0600
Raw View
mtshea@sdcc10.ucsd.edu (Matthew Shea) writes:

>Does any one how to overload the [][] operator??.. I don't think
>that you can do it, so does any one know how to overload the comma
>operator so that 2 parameters could be passed in the [] operator,
>like [10,3] would represent [10][3].. The reason I want to do this
>is that I make a two dimensional triagular array class, and I want
>to be able to use it, or very similarly like, a two _d rectangular
>array.  I already finished the class with an accessor function, but
>I want it to be more abstract from the user.

There is no [][] operator. However you could devise an auxilliary
class to act as an intermediate operand. You would then be able
to write triangular[sub1][sub2] and have it translated to

(triangular.operator[](sub1)).operator[](sub2)

The result of (triangular.operator[](sub1) would have to be
an object for which operator[] has meaning; in the simplest case
it could even be a pointer to the proper subarray.

The comma operator is unusable for this purpose, as it cannot be
overloaded unless one of its operands is a class object or reference.

You might consider using parentheses for multiple subscripting, as
operator() can be overloaded with any number of arguments.




Author: tob@world.std.com (Tom O Breton)
Date: Tue, 8 Nov 1994 21:49:04 GMT
Raw View
mtshea@sdcc10.ucsd.edu (Matthew Shea) writes:
> Does any one how to overload the [][] operator??.. I don't think
> that you can do it, so does any one know how to overload the comma
> operator so that 2 parameters could be passed in the [] operator,

There was some discussion a few months back proposing an (or a family
of) operator[][], but there is no such thing in C++ as it stands or as
it is planned for the foreseeable future.

        Tom

--
finger me for how Tehomega is coming along (at tob@world.std.com)
Author of The Burning Tower (from TomBreton@delphi.com) (weekly in
rec.games.frp.archives)





Author: tarmstro@caleuche.dis.udec.cl (Thomas Armstrong Kobrich)
Date: Wed, 09 Nov 1994 21:26:08 GMT
Raw View
Matthew Shea (mtshea@sdcc10.ucsd.edu) wrote:
> Does any one how to overload the [][] operator??.. I don't think
> that you can do it, so does any one know how to overload the comma
> operator so that 2 parameters could be passed in the [] operator,
> like [10,3] would represent [10][3].. The reason I want to do this

 I don't know if it works, but try:

  #define ][ ,

 (is this legal??)
--
-----------------------------------
| Thomas Armstrong Kobrich        |
| tarmstro@caleuche.dis.udec.cl   |
| Concepcion - Chile              |
-----------------------------------