By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,900 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,900 IT Pros & Developers. It's quick & easy.

Can operator [] accept two arguments?

P: n/a
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

Nov 5 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Nov 5 '05 #2

P: n/a
Pe*******@gmail.com <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?


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

--
Marcus Kwok
Nov 6 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.