Connecting Tech Pros Worldwide Forums | Help | Site Map

Can operator [] accept two arguments?

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Nov 5 '05
I'm wondering whether the operator [] can accept more than 1 arguments

Suppose I have a object which is essentially a 2 dimensional array, I
want to use operator [] to access the data. I don't what to use [] to
return a 1 dimentional array and use another [] to access the data in
this 1 dimentional array.

Is it possible to accept 2 arguments?

Thanks,
Peng


Greg
Guest
 
Posts: n/a
#2: Nov 5 '05

re: Can operator [] accept two arguments?


PengYu.UT@gmail.com wrote:[color=blue]
> I'm wondering whether the operator [] can accept more than 1 arguments
>
> Suppose I have a object which is essentially a 2 dimensional array, I
> want to use operator [] to access the data. I don't what to use [] to
> return a 1 dimentional array and use another [] to access the data in
> this 1 dimentional array.
>
> Is it possible to accept 2 arguments?
>
> Thanks,
> Peng[/color]

No, an overloaded subscript operator accepts only one argument (which
for the overloaded operator can be of any type, not necessarily an
integral type).

It is possible to have the [] operator return a reference to the object
itself, or some other object that also implements the [] operator.
Doing so would allow multiple subscript operations to be chained:

MyObject[index1][index2];

Alternately, instead of overloading the [] operator, overload the ()
operator instead. The () operator can be declared to accept any number
of arguments. The only meaningful difference between between the
overloaded versions is the difference between having round or having
square brackets.

Greg

Marcus Kwok
Guest
 
Posts: n/a
#3: Nov 6 '05

re: Can operator [] accept two arguments?


PengYu.UT@gmail.com <PengYu.UT@gmail.com> wrote:[color=blue]
> I'm wondering whether the operator [] can accept more than 1 arguments
>
> Suppose I have a object which is essentially a 2 dimensional array, I
> want to use operator [] to access the data. I don't what to use [] to
> return a 1 dimentional array and use another [] to access the data in
> this 1 dimentional array.
>
> Is it possible to accept 2 arguments?[/color]

No, see
http://www.parashift.com/c++-faq-lit...html#faq-13.10

--
Marcus Kwok
Closed Thread