Pe*******@gmail.com wrote:
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
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