Can operator [] accept two arguments?

Discussion in 'C++' started by PengYu.UT@gmail.com, Nov 5, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Greg Guest

    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
    Greg, Nov 5, 2005
    #2
    1. Advertising

  3. Marcus Kwok Guest

    <> 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-lite/operator-overloading.html#faq-13.10

    --
    Marcus Kwok
    Marcus Kwok, Nov 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    2
    Views:
    2,996
  2. GenxLogic
    Replies:
    3
    Views:
    1,272
    andrewmcdonagh
    Dec 6, 2006
  3. vivekian
    Replies:
    7
    Views:
    318
    vivekian
    Nov 25, 2005
  4. Gernot Frisch
    Replies:
    6
    Views:
    343
    Gernot Frisch
    Nov 29, 2005
  5. TOMERDR
    Replies:
    5
    Views:
    496
    Diego Martins
    Apr 4, 2006
Loading...

Share This Page