how to overload subscript of 2D-array

Discussion in 'C++' started by DaVinci, May 10, 2006.

  1. DaVinci

    DaVinci Guest

    /* how to overload the operation [] ,subscipt of 2D-array?
    * how can I get element through piece[j],rather than piece(i)[j]?
    * */
    #include"global.h"
    using namespace std;
    class Piece
    {
    public:
    Piece()
    {
    for(size_t i = 0;i<2;i++)
    for(size_t j = 0;j<3;j++)
    {
    piece[j] = i+ j;
    }
    for(size_t i=0;i<2;i++)
    {
    std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
    cout<<endl;
    }
    cout<<"----------------------"<<endl;
    }
    int* operator()(const int& t)//change operator() to operator[] is
    wrong ,why?
    {
    i = t; //
    return piece;
    }
    int operator[](const int & t)
    {
    return this->operator()(i)[t];
    }
    private:
    int i;//witchout varible i,how can I implement overload the () , []
    or[],[]
    int piece[2][3];
    };
    //----------------------------------------
    int main()
    {
    Piece p ;
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<3;j++)
    {
    cout<<p(i)[j]<<" ";//p[j],??
    }
    cout<<endl;
    }
    }
    /*
    //output is
    0 1 2
    1 2 3
    ----------------------
    0 1 2
    1 2 3
    */
     
    DaVinci, May 10, 2006
    #1
    1. Advertising

  2. DaVinci wrote:
    > /* how to overload the operation [] ,subscipt of 2D-array?
    > * how can I get element through piece[j],rather than piece(i)[j]?
    > * */


    This is a FAQ, http://www.parashift.com/c -faq-lite/.


    Jonathan
     
    Jonathan Mcdougall, May 10, 2006
    #2
    1. Advertising

  3. DaVinci

    Axter Guest

    DaVinci wrote:
    > /* how to overload the operation [] ,subscipt of 2D-array?
    > * how can I get element through piece[j],rather than piece(i)[j]?
    > * */
    > #include"global.h"
    > using namespace std;
    > class Piece
    > {
    > public:
    > Piece()
    > {
    > for(size_t i = 0;i<2;i++)
    > for(size_t j = 0;j<3;j++)
    > {
    > piece[j] = i+ j;
    > }
    > for(size_t i=0;i<2;i++)
    > {
    > std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
    > cout<<endl;
    > }
    > cout<<"----------------------"<<endl;
    > }
    > int* operator()(const int& t)//change operator() to operator[] is
    > wrong ,why?
    > {
    > i = t; //
    > return piece;
    > }
    > int operator[](const int & t)
    > {
    > return this->operator()(i)[t];
    > }
    > private:
    > int i;//witchout varible i,how can I implement overload the () , []
    > or[],[]
    > int piece[2][3];
    > };
    > //----------------------------------------
    > int main()
    > {
    > Piece p ;
    > for(int i=0;i<2;i++)
    > {
    > for(int j=0;j<3;j++)
    > {
    > cout<<p(i)[j]<<" ";//p[j],??
    > }
    > cout<<endl;
    > }
    > }
    > /*
    > //output is
    > 0 1 2
    > 1 2 3
    > ----------------------
    > 0 1 2
    > 1 2 3
    > */



    I recommend against using the method posted in the C++ FAQ.
    It recommends you use a non-standard syntax.

    If you want to use standard syntax check out the following link for an
    example:
    http://code.axter.com/dynamic_2d_array.h

    However, for most requirements, I recommend using a vector of vector.
    Example:
    int col = 123;
    int row = 456;
    vector<vector<int> > My2dArray(col, vector<int>(row));


    You can reference both the above vector code and the dynamic_2d_array
    class using double index ([][])
    My2dArray[0][0] = 99;

    Check out the following link for wrapper classes using vector of
    vector:
    http://www.codeguru.com/forum/showthread.php?t=231046
    http://www.codeguru.com/forum/showthread.php?s=&threadid=297838
     
    Axter, May 10, 2006
    #3
  4. DaVinci

    DaVinci Guest

    Axter wrote:

    > DaVinci wrote:
    > > /* how to overload the operation [] ,subscipt of 2D-array?
    > > * how can I get element through piece[j],rather than piece(i)[j]?
    > > * */
    > > #include"global.h"
    > > using namespace std;
    > > class Piece
    > > {
    > > public:
    > > Piece()
    > > {
    > > for(size_t i = 0;i<2;i++)
    > > for(size_t j = 0;j<3;j++)
    > > {
    > > piece[j] = i+ j;
    > > }
    > > for(size_t i=0;i<2;i++)
    > > {
    > > std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
    > > cout<<endl;
    > > }
    > > cout<<"----------------------"<<endl;
    > > }
    > > int* operator()(const int& t)//change operator() to operator[] is
    > > wrong ,why?
    > > {
    > > i = t; //
    > > return piece;
    > > }
    > > int operator[](const int & t)
    > > {
    > > return this->operator()(i)[t];
    > > }
    > > private:
    > > int i;//witchout varible i,how can I implement overload the () , []
    > > or[],[]
    > > int piece[2][3];
    > > };
    > > //----------------------------------------
    > > int main()
    > > {
    > > Piece p ;
    > > for(int i=0;i<2;i++)
    > > {
    > > for(int j=0;j<3;j++)
    > > {
    > > cout<<p(i)[j]<<" ";//p[j],??
    > > }
    > > cout<<endl;
    > > }
    > > }
    > > /*
    > > //output is
    > > 0 1 2
    > > 1 2 3
    > > ----------------------
    > > 0 1 2
    > > 1 2 3
    > > */

    >
    >
    > I recommend against using the method posted in the C++ FAQ.
    > It recommends you use a non-standard syntax.
    >
    > If you want to use standard syntax check out the following link for an
    > example:
    > http://code.axter.com/dynamic_2d_array.h
    >
    > However, for most requirements, I recommend using a vector of vector.
    > Example:
    > int col = 123;
    > int row = 456;
    > vector<vector<int> > My2dArray(col, vector<int>(row));
    >
    >
    > You can reference both the above vector code and the dynamic_2d_array
    > class using double index ([][])
    > My2dArray[0][0] = 99;
    >
    > Check out the following link for wrapper classes using vector of
    > vector:
    > http://www.codeguru.com/forum/showthread.php?t=231046
    > http://www.codeguru.com/forum/showthread.php?s=&threadid=297838


    yes,I know vector<vector<int> > but here

    The class Piece 's purpose 's isnot just for 2D-array,it was desiged to
    support some other
    operation such as void draw_grahic(),
    void move_left() and so on.
    But I must first Encapsulate the 2d-array int piece[2][3] as private
    data.
    and supply operator[][] to get it's data,
    is there way to overload the operator[] twice to meet my need?
     
    DaVinci, May 10, 2006
    #4
  5. DaVinci wrote:
    > The class Piece 's purpose 's isnot just for 2D-array,it was desiged to
    > support some other
    > operation such as void draw_grahic(),
    > void move_left() and so on.
    > But I must first Encapsulate the 2d-array int piece[2][3] as private
    > data.
    > and supply operator[][] to get it's data,
    > is there way to overload the operator[] twice to meet my need?


    If that's what you really want, just make an operator[] that returns a
    proxy. Then, put another operator[] in that proxy that gives a value:

    # include <vector>
    # include <cstddef>

    class test;

    class proxy
    {
    friend class test;

    public:
    int operator[](std::size_t index)
    {
    return v_[index];
    }

    private:
    proxy(std::vector<int>& v)
    : v_(v)
    {
    }

    std::vector<int>& v_;
    };


    class test
    {
    public:
    proxy operator[](std::size_t index)
    {
    return proxy(v_[index]);
    }

    private:
    std::vector< std::vector<int> > v_;
    };

    int main()
    {
    test t;
    int i = t[1][2];
    }

    This is completly artificial, since std::vector already has a subscript
    operator, but you should get the picture.


    Jonathan
     
    Jonathan Mcdougall, May 10, 2006
    #5
  6. DaVinci

    Axter Guest

    DaVinci wrote:
    > Axter wrote:
    >
    > > DaVinci wrote:
    > > > /* how to overload the operation [] ,subscipt of 2D-array?
    > > > * how can I get element through piece[j],rather than piece(i)[j]?
    > > > * */
    > > > #include"global.h"
    > > > using namespace std;
    > > > class Piece
    > > > {
    > > > public:
    > > > Piece()
    > > > {
    > > > for(size_t i = 0;i<2;i++)
    > > > for(size_t j = 0;j<3;j++)
    > > > {
    > > > piece[j] = i+ j;
    > > > }
    > > > for(size_t i=0;i<2;i++)
    > > > {
    > > > std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
    > > > cout<<endl;
    > > > }
    > > > cout<<"----------------------"<<endl;
    > > > }
    > > > int* operator()(const int& t)//change operator() to operator[] is
    > > > wrong ,why?
    > > > {
    > > > i = t; //
    > > > return piece;
    > > > }
    > > > int operator[](const int & t)
    > > > {
    > > > return this->operator()(i)[t];
    > > > }
    > > > private:
    > > > int i;//witchout varible i,how can I implement overload the () , []
    > > > or[],[]
    > > > int piece[2][3];
    > > > };
    > > > //----------------------------------------
    > > > int main()
    > > > {
    > > > Piece p ;
    > > > for(int i=0;i<2;i++)
    > > > {
    > > > for(int j=0;j<3;j++)
    > > > {
    > > > cout<<p(i)[j]<<" ";//p[j],??
    > > > }
    > > > cout<<endl;
    > > > }
    > > > }
    > > > /*
    > > > //output is
    > > > 0 1 2
    > > > 1 2 3
    > > > ----------------------
    > > > 0 1 2
    > > > 1 2 3
    > > > */

    > >
    > >
    > > I recommend against using the method posted in the C++ FAQ.
    > > It recommends you use a non-standard syntax.
    > >
    > > If you want to use standard syntax check out the following link for an
    > > example:
    > > http://code.axter.com/dynamic_2d_array.h
    > >
    > > However, for most requirements, I recommend using a vector of vector.
    > > Example:
    > > int col = 123;
    > > int row = 456;
    > > vector<vector<int> > My2dArray(col, vector<int>(row));
    > >
    > >
    > > You can reference both the above vector code and the dynamic_2d_array
    > > class using double index ([][])
    > > My2dArray[0][0] = 99;
    > >
    > > Check out the following link for wrapper classes using vector of
    > > vector:
    > > http://www.codeguru.com/forum/showthread.php?t=231046
    > > http://www.codeguru.com/forum/showthread.php?s=&threadid=297838

    >
    > yes,I know vector<vector<int> > but here
    >
    > The class Piece 's purpose 's isnot just for 2D-array,it was desiged to
    > support some other
    > operation such as void draw_grahic(),
    > void move_left() and so on.
    > But I must first Encapsulate the 2d-array int piece[2][3] as private
    > data.
    > and supply operator[][] to get it's data,
    > is there way to overload the operator[] twice to meet my need?


    Yes.
    Did you look at the first link I posted?
    http://code.axter.com/dynamic_2d_array.h

    The above link doesn't use vector vector, and it's able to use
    operator[] to return a 2D array without using a proxy class.
     
    Axter, May 10, 2006
    #6
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  2. Tom Page
    Replies:
    4
    Views:
    465
    Victor Bazarov
    Feb 17, 2004
  3. Richard Delorme

    out of range array subscript

    Richard Delorme, May 3, 2004, in forum: C Programming
    Replies:
    5
    Views:
    482
    Chris Torek
    May 15, 2004
  4. pasa_1
    Replies:
    2
    Views:
    340
    Thomas J. Gritzan
    Aug 9, 2006
  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