Thread: Overloading subscript operator for multi-dimensional array.

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798

    Overloading subscript operator for multi-dimensional array.

    How do I overload the subscript operator for multi-dimensions?

    Would the signature need to be

    Code:
    int& MyArray::operator [][] (int y, int x);
    or

    Code:
    int& MyArray::operator [] (int y, int x);
    or are none of these correct?

    I have tried a few things but I get errors all the time.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    operator [] can only take 1 parameter, so you have 2 options. Either overload operator() or overload operator[] to return the address of the first element of a row in the matrix.

    This was actually answered a couple days ago (refer to the last few posts in that thread)

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    1

    It is possible to implement double subscript operator

    Quote Originally Posted by Polymorphic OOP View Post
    so you have 2 options.
    There is yet another option that is very elegant for the end user, but requires more work on your end.

    If you create a nested class/struct within your class that represents the second dimension of the data, you can achieve the desired behavior.

    Let me explain by pseudo-code example. Lets create a class called TwoD which stores FOO objects in an array called FooArray.
    We wish to offer access to our FooArrary data-member through variable.[1][1] syntax.

    Code:
    const int WIDTH = 20;
    const int HEIGHT = 20;
    Class TwoD
    {
      private:
      FOO [WIDTH * HEIGHT];
      BAR [HEIGHT][WIDTH];  /* order doesn't matter. The important thing is that you pick one way of viewing the order. X,Y is just as correct as Y,X as long as you're consistant. */
    
      struct OneDFoo
      {
           FOO* sourceArrary; //data-member
    
           OneD(FOO* source) : sourceArray(source) {};  /* constructor, which stores the source array to index into when subscript operator is called */
    
           FOO& operator[](int index) //Operator for second level of indirection
           {
             return sourceArray[index];
           }
      }//end of OneDFoo struct
    
    
     public:
      //Constructor & Destructor...
    
     FOO& operator[](int index)
     {
       int offset = index*WIDTH; /* we multiply the first index by the number of indecies wide the array is so that we are moved into the array the proper "height" and can now continue indexing to locate the "width" */
    
       OneDFoo temp(&FOO[offset]);  /* initialize the one-d portion with the address of the array starting from the proper height (offset) */
    
       return temp[index]; /* use the subscript operator of the OneDFoo struct, which will navigate index number of FOO objects over, and return the one that was found, which we then return out of the whole class */
     }
    
    
      //other methods...
    
    //Example with the BAR data-member
    
      struct OneDBar
      {
           BAR* sourceArrary; //data-member
    
           OneD(BAR* source) : sourceArray(source) {};  /* constructor, which stores the source array to index into when subscript operator is called */
    
           BAR& operator[](int index) //Operator for second level of indirection
           {
             return sourceArray[index];
           }
      }//end of OneDBar struct
    
    
     public:
      //Constructor & Destructor...
    
     BAR& operator[](int index)
     {
       OneDBar temp(BAR[index]);  /* THIS IS THE SIGNIFICANT DIFFERENCE FROM THE FOO EXAMPLE */
       return temp[index]; 
     }
    
    
    }//end of TwoD class
    I can't guarentee this has no typos, or that it will compile as I only meant to provide a usefull pseudo-code like example.
    Also this is more verbose than I would actually implement it, which was done on purpose to explain things, as well
    this only covers non const operator [] you will need also need to implement the const version unless your project does not
    require it, and finally in the real world this would be templatized.

    Hope this provides insight.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or indeed reading forum rules about digging up threads that are 8 YEARS old.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM