Topic: operator[] with more than one argument (solution)


Author: roshan_naik@yahoo.com
Date: Wed, 21 Aug 2002 22:48:57 GMT
Raw View
It is possible to do this within the language.

Here is how....
  1)  overload operator comma to return a pair
   2) then overload operator [ ]  for a pair

That allows you to use this syntax....

           tab[ x, y ]     // assume operator , ()   is overloaded on types
of x and y

This technique can be extended to  allow any number of arguments to [ ] by
using pair , triple, quadruple etc.

I highly doubt that a langauge extension is warranted for this.

--Roshan Naik


John Nagle wrote:

>     Is there any good reason why "operator[]" couldn't be
> permitted to take more than one argument, so that
>
>                 tab[i,j]
>
> would be meaningful for classes that chose to support it?
>
>                                 John Nagle
>                                 Animats
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ken Alverson" <Ken@Alverson.net>
Date: Thu, 22 Aug 2002 04:35:39 GMT
Raw View
<roshan_naik@yahoo.com> wrote in message
news:3D62A7B2.DB77C59C@yahoo.com...
> It is possible to do this within the language.
>
> Here is how....
>   1)  overload operator comma to return a pair
>    2) then overload operator [ ]  for a pair

Except you can't overload operator,(int,int).  Which negates the most
likely desired use.  The closest you can really come is:

index2 operator,(index a, int b) {return index2(a,b);}
index3 operator,(index2 a, int b) {return index3(a,b);}
...

myIndexableContainer[index(a),b,c];

which isn't /too/ hideous, but it isn't all that pretty either.  At that
point, you might as well stop overloading the comma and use

myIndexableContainer[index3(a,b,c)];

Ken


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: John Nagle <nagle@animats.com>
Date: Thu, 22 Aug 2002 16:42:37 GMT
Raw View
roshan_naik@yahoo.com wrote:

> It is possible to do this within the language.
>
> Here is how....
>   1)  overload operator comma to return a pair
>    2) then overload operator [ ]  for a pair


     Doesn't work.  You can't overload

  operator,(int,int)

because at least one type in an operator overload
must be a class type.  You're not allowed to overload
operators for the built-in types.

     (And if you could, you'd
change the semantics of "operator," everywhere,
not just within square brackets.)

    John Nagle
    Animats

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]