How can I make a Two-Dimension array of classes by operator overloading?

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

  1. adc++

    adc++ Guest

    Hi
    Imagine that I have two classes one columns and one rows.
    I want to can call a class-pointer that is in the rows with this call:
    man [5][4]
    how can I do it?
    I know that I must overload [] but this have two dimensions. How can I
    do it?
    adc++, May 10, 2006
    #1
    1. Advertising

  2. adc++ wrote:
    > Hi
    > Imagine that I have two classes one columns and one rows.
    > I want to can call a class-pointer that is in the rows with this call:
    > man [5][4]
    > how can I do it?
    > I know that I must overload [] but this have two dimensions. How can I
    > do it?


    Funny, two identical questions in 10 minutes.

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


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

  3. adc++

    Axter Guest

    adc++ wrote:
    > Hi
    > Imagine that I have two classes one columns and one rows.
    > I want to can call a class-pointer that is in the rows with this call:
    > man [5][4]
    > how can I do it?
    > I know that I must overload [] but this have two dimensions. How can I
    > do it?


    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
    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. Larry Lindsey
    Replies:
    5
    Views:
    525
    Mike Wahler
    Sep 27, 2003
  2. James
    Replies:
    11
    Views:
    93,845
    yousafzai
    Jun 4, 2011
  3. James
    Replies:
    3
    Views:
    358
    E. Robert Tisdale
    Nov 2, 2004
  4. ssss
    Replies:
    2
    Views:
    371
    -berlin.de
    Nov 19, 2004
  5. Luuk
    Replies:
    15
    Views:
    804
    Nobody
    Feb 11, 2010
Loading...

Share This Page