overload []

Discussion in 'C++' started by Denis Remezov, May 8, 2004.

  1. Mario Charest wrote:
    >
    > .
    > Hi,
    >
    > It's possible to overload the [] operator, but I wonder if it's possible to
    > somehow overload [][] and so forth.
    >
    > My goal would be to switch from static array declaration into something
    > totaly dynamic with minimal change in the code.
    >
    > The following code
    >
    > int foo[NB_ROW][NB_COL];
    >
    > foo[12][3] = 0;
    >
    > would become:
    >
    > Array2DimInt foo(NB_ROW, NB_COL);
    >
    > foo[12][3] = 0;
    >


    A vector<vector<int> > that Dan suggested would work, but it is a bit too
    generic for matrices (each row stores its own dimension, which you need
    to care about). In some cases its suboptimal memory usage can be a factor
    as well. Here are two more choices:

    a) (Sean beat me to it, but I'm going to post this anyway since I've
    typed it already).
    Make the first [] return a proxy object. Here is an example idea
    (it didn't feel right not making it a template):

    template <typename T>
    class Array2Dim {
    vector<T> data_;
    int width_;
    public:
    class RowProxy {
    friend class Array2Dim;
    vector<T>& data_;
    int ofs_;
    RowProxy(vector<T>& data, int ofs) : data_(data), ofs_(ofs) {}
    public:
    T& operator [](int col) {
    return data_[ofs_+col];
    }
    };

    RowProxy operator [](int row) {
    return RowProxy(data_, row*width_);
    }
    //...
    };

    b) Consider using the (i, j) notation instead of [j]. I prefer the
    [j] syntax, but (i, j) is superior in its implementation.
    (Use operator() (int row, int col)).
    Though as you mentioned you wanted to minimise the change to the existing
    code, this may not work well for you.

    Denis
     
    Denis Remezov, May 8, 2004
    #1
    1. Advertising

  2. ..
    Hi,

    It's possible to overload the [] operator, but I wonder if it's possible to
    somehow overload [][] and so forth.

    My goal would be to switch from static array declaration into something
    totaly dynamic with minimal change in the code.

    The following code

    int foo[NB_ROW][NB_COL];

    foo[12][3] = 0;

    would become:

    Array2DimInt foo(NB_ROW, NB_COL);

    foo[12][3] = 0;

    Am I making sense?

    - Mario
     
    Mario Charest, May 8, 2004
    #2
    1. Advertising

  3. John Harrison wrote:
    >
    > "Mario Charest" <postmaster@127.0.0.1> wrote in message
    > news:YyWmc.78569$...
    > > .
    > > Hi,
    > >
    > > It's possible to overload the [] operator, but I wonder if it's possible

    > to
    > > somehow overload [][] and so forth.
    > >
    > > My goal would be to switch from static array declaration into something
    > > totaly dynamic with minimal change in the code.
    > >
    > > The following code
    > >
    > > int foo[NB_ROW][NB_COL];
    > >
    > > foo[12][3] = 0;
    > >
    > > would become:
    > >
    > > Array2DimInt foo(NB_ROW, NB_COL);
    > >
    > > foo[12][3] = 0;
    > >
    > > Am I making sense?
    > >
    > > - Mario
    > >

    >
    > The proxy class idea that others have suggested is a good general purpose
    > solution.
    >
    > However in your particular case there is a simpler answer. Just have your
    > operator[] return a pointer the start of a row. So in an expression like
    >
    > foo[12][3]
    >
    > the first operator[] is your operator[] for the class, and the second
    > operator[] is just the regular operator[] that works on the pointer returned
    > by the first operator[].
    >
    > john


    Thanks! I completely missed it. The part I like the most is that you still can
    use a vector as one contiguous storage.

    Denis
     
    Denis Remezov, May 8, 2004
    #3
  4. Denis Remezov

    Dan Cernat Guest

    "Mario Charest" <postmaster@127.0.0.1> wrote in message
    news:YyWmc.78569$...
    > .
    > Hi,
    >
    > It's possible to overload the [] operator, but I wonder if it's possible

    to
    > somehow overload [][] and so forth.
    >
    > My goal would be to switch from static array declaration into something
    > totaly dynamic with minimal change in the code.
    >
    > The following code
    >
    > int foo[NB_ROW][NB_COL];
    >
    > foo[12][3] = 0;
    >
    > would become:
    >
    > Array2DimInt foo(NB_ROW, NB_COL);
    >
    > foo[12][3] = 0;
    >
    > Am I making sense?
    >
    > - Mario
    >


    try

    using namespace std;

    vector < vector < int> > foo;

    it is totally dynamic. after you have data in, you can use it like:

    foo[12][3] = 42;

    however, the indexes shouldn't be out of range
     
    Dan Cernat, May 8, 2004
    #4
  5. Denis Remezov

    Woebegone Guest

    "Mario Charest" <postmaster@127.0.0.1> wrote in message
    news:YyWmc.78569$...
    > .
    > Hi,
    >
    > It's possible to overload the [] operator, but I wonder if it's possible

    to
    > somehow overload [][] and so forth.
    >
    > My goal would be to switch from static array declaration into something
    > totaly dynamic with minimal change in the code.
    >
    > The following code
    >
    > int foo[NB_ROW][NB_COL];
    >
    > foo[12][3] = 0;
    >
    > would become:
    >
    > Array2DimInt foo(NB_ROW, NB_COL);
    >
    > foo[12][3] = 0;
    >
    > Am I making sense?
    >
    > - Mario
    >
    >
    >
    >

    Hi Mario,

    It's not possible to directly overload [][]. However, you can overload [] to
    return a type for which [] is also meaningful, thus synthesizing [][] for
    the enclosing class. That's what Dan's sol'n does for you, using the
    standard lib so you don't have to do the work.

    Sean.
     
    Woebegone, May 8, 2004
    #5
  6. "Mario Charest" <postmaster@127.0.0.1> wrote in message
    news:YyWmc.78569$...
    > .
    > Hi,
    >
    > It's possible to overload the [] operator, but I wonder if it's possible

    to
    > somehow overload [][] and so forth.
    >
    > My goal would be to switch from static array declaration into something
    > totaly dynamic with minimal change in the code.
    >
    > The following code
    >
    > int foo[NB_ROW][NB_COL];
    >
    > foo[12][3] = 0;
    >
    > would become:
    >
    > Array2DimInt foo(NB_ROW, NB_COL);
    >
    > foo[12][3] = 0;
    >
    > Am I making sense?
    >
    > - Mario
    >


    The proxy class idea that others have suggested is a good general purpose
    solution.

    However in your particular case there is a simpler answer. Just have your
    operator[] return a pointer the start of a row. So in an expression like

    foo[12][3]

    the first operator[] is your operator[] for the class, and the second
    operator[] is just the regular operator[] that works on the pointer returned
    by the first operator[].

    john
     
    John Harrison, May 8, 2004
    #6
  7. "John Harrison" <> wrote in message
    news:...
    >
    > "Mario Charest" <postmaster@127.0.0.1> wrote in message
    > news:YyWmc.78569$...
    > > .
    > > Hi,
    > >
    > > It's possible to overload the [] operator, but I wonder if it's possible

    > to
    > > somehow overload [][] and so forth.
    > >
    > > My goal would be to switch from static array declaration into something
    > > totaly dynamic with minimal change in the code.
    > >
    > > The following code
    > >
    > > int foo[NB_ROW][NB_COL];
    > >
    > > foo[12][3] = 0;
    > >
    > > would become:
    > >
    > > Array2DimInt foo(NB_ROW, NB_COL);
    > >
    > > foo[12][3] = 0;
    > >
    > > Am I making sense?
    > >
    > > - Mario
    > >

    >
    > The proxy class idea that others have suggested is a good general purpose
    > solution.
    >
    > However in your particular case there is a simpler answer. Just have your
    > operator[] return a pointer the start of a row. So in an expression like
    >
    > foo[12][3]
    >
    > the first operator[] is your operator[] for the class, and the second
    > operator[] is just the regular operator[] that works on the pointer

    returned
    > by the first operator[].


    I like the simplicity of the solution but it would not work in my case.

    The arrays are used to map an image coming from a camera. Depending on
    various factor the camera could be rotate 90 180 or 270 degrees. The
    software current has different code path to handle the different angle, it's
    a pain to maintain. I would like foo[12][3] to return the same pixel value
    what ever the image angle (rotating the image beforehand is out of the
    question for performance reason).

    In that case using vector is out of the question. I think what I'll end up
    doing is an image class with a Pixel( int x, int y ) member and I'll have a
    class per angle. This means lots of change in the software but it doesn't
    look like I have a choice.

    Seems to me like only being able to overload [] is an oversight. If you can
    overload one [] you should be able to overload [][], oh well.

    Thanks everyone.


    >
    > john
    >
    >
     
    Mario Charest, May 8, 2004
    #7
  8. "Woebegone" <> wrote in message
    news:rPYmc.4418$...
    >
    > "Mario Charest" <postmaster@127.0.0.1> wrote in message
    > news:YyWmc.78569$...
    > > .
    > > Hi,
    > >
    > > It's possible to overload the [] operator, but I wonder if it's possible

    > to
    > > somehow overload [][] and so forth.
    > >
    > > My goal would be to switch from static array declaration into something
    > > totaly dynamic with minimal change in the code.
    > >
    > > The following code
    > >
    > > int foo[NB_ROW][NB_COL];
    > >
    > > foo[12][3] = 0;
    > >
    > > would become:
    > >
    > > Array2DimInt foo(NB_ROW, NB_COL);
    > >
    > > foo[12][3] = 0;
    > >
    > > Am I making sense?
    > >
    > > - Mario
    > >
    > >
    > >
    > >

    > Hi Mario,
    >
    > It's not possible to directly overload [][]. However, you can overload []

    to
    > return a type for which [] is also meaningful, thus synthesizing [][] for
    > the enclosing class. That's what Dan's sol'n does for you, using the
    > standard lib so you don't have to do the work.


    I'll try it out thanks.

    >
    > Sean.
    >
    >
     
    Mario Charest, May 8, 2004
    #8
  9. Denis Remezov

    Jorge Rivera Guest


    > Seems to me like only being able to overload [] is an oversight. If you can
    > overload one [] you should be able to overload [][], oh well.
    >
    > Thanks everyone.
    >


    I disagree with this. Can't you use arbitrarily nesting of [][][] in C++?

    Then your suggestion implies that in order for a custom class to use
    this syntax, you are required to have an overload per level.
    n-dimension array requiring n overloaded operators?

    Doesn't sound all that good.

    I your deciion of using operator() instead of operator[] is the way to go.

    Good luck.

    Jorge L.
    Jorge L.
     
    Jorge Rivera, May 8, 2004
    #9
  10. "Jorge Rivera" <> wrote in message
    news:Vc7nc.15634$...
    >
    > > Seems to me like only being able to overload [] is an oversight. If you

    can
    > > overload one [] you should be able to overload [][], oh well.
    > >
    > > Thanks everyone.
    > >

    >
    > I disagree with this. Can't you use arbitrarily nesting of [][][] in C++?
    >
    > Then your suggestion implies that in order for a custom class to use
    > this syntax, you are required to have an overload per level.
    > n-dimension array requiring n overloaded operators?
    >
    > Doesn't sound all that good.
    >
    > I your deciion of using operator() instead of operator[] is the way to go.
    >
    > Good luck.
    >
    > Jorge L.
    > Jorge L.


    You can also do it with clever use of templates. This avoids the need to
    write one class for each dimension of your n-dimensional array. Instead you
    have a template with a integer parameter representing the array dimension.

    Look at the boost MultiArray class for an example,
    http://www.boost.org/libs/multi_array/doc/index.html

    john
     
    John Harrison, May 8, 2004
    #10
    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. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,866
    Eph0nk
    Oct 23, 2003
  2. MattB

    Overload

    MattB, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    547
    MattB
    Dec 23, 2003
  3. Babs
    Replies:
    0
    Views:
    3,066
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page