I simply have to overload square brackets in order for a program to work. I know the syntax for 1-dimensional array. It goes something like this:

class MyArray
{
   private:
       int *arr;
       int size;
    public:
       MyArray(int s)
       {
            size = s;
            arr = new int[size];
       }
       int & MyArray operator[](int);    // declaring
};

int & MyArray :: operator[](int subscript)
{
    assert(0 <= subscript && subscript <= size)    
    return arr[subscript];
}

void main()
{
  MyArray a1(5);

   a1[2] = 3;

}

I've included just the basic functions in this. This works fine. I just can't think of how to apply this to a double dimension array. I desperately need to overload [] for a 2 dimensional array. Can somebody please help?

Edited 3 Years Ago by Dani: Formatting fixed

>I simply have to overload square brackets in order for a program to work.
Why? It's considerably easier to overload the () operator to take two arguments instead of hack your way to a solution with the [] operator. But if you must know, you need to use a helper class:

// Pseudocode
template <typename T>
class helper {
public:
  const T operator[](int j) const;
  T& operator[](int j);
};

template <typename T>
class matrix {
public:
  const helper operator[](int i) const;
  helper& operator[](int i);
};

//Why?

Well coz I want the convenience of declaring a 2-dimensional array as :

MyArray obj;

and then also be able to assign values directly such as :

obj [2][3] = 5;

I'm gonna try what you suggested though. Thanks.

>Well coz I want the convenience
It's not as convenient as you seem to think it is. Why aren't you using vectors anyway? You get the "convenient" syntax out of it and it'll probably be a lot more efficient that anything you can write.

It's not as convenient as you seem to think it is. Why aren't you using vectors anyway? You get the "convenient" syntax out of it and it'll probably be a lot more efficient that anything you can write.

Well I do see your point. But my problem is solved with overloading (). I didn't really want to use () if there was a solution using [] alone. So I thought might as well check it out.
Vectors - yes. It didn't occur to me to use them as I'm new to C++. Besides it's too late to change my code now.

>Besides it's too late to change my code now.
Why? Has the military started accepting code from newbies and you've passed your deadline? "It's too late to change my code" is a really lame excuse for shoddy workmanship. In fact, if you change your code, you learn more than if you hadn't. Since you're new to C++, that's a very good thing. But no, you're too stubborn to change your code for the better. Congratulations! You're well on your way to mediocrity.

"It's too late to change my code" is a really lame excuse for shoddy workmanship. In fact, if you change your code, you learn more than if you hadn't. Since you're new to C++, that's a very good thing. But no, you're too stubborn to change your code for the better.

Maybe it is...but when I've got a deadline which ends tomorrow and will flunk if I don't have my code in by then, learning can take a backseat...at least for now. I'm not stubborn, just a victim of time constraints!

>I'm not stubborn, just a victim of time constraints!
I have yet to see a class project that doesn't give you more than enough time to finish. If you're approaching the deadline then it's your own fault for either being too lazy to do the work, or too braindead to get a simple solution working before trying to do something fancy. Let that be the lesson learned for this project.

got to admit that i have tried it myself once - but that lasted about 30 secs. IMHO There is NEVER a need to overload more than simple [] with an index supplied. There are so many alternatives and easier coding methods than trying to overload [][]! if you HAVE to implement an array then store another variable to refer to its number of dimensions. then overload [] to take two params, the dimension and the index. Im sure with a pen and paper you can work out how to return the correct element..... :)

@Narue: ive had a look at the snippets on STL vectors and am starting to get a grasp of them but do you know of any in depth tutorials for the subject as it would be good to know if they will help me with any of my current coding projects?

>@Narue:
I don't know of any good ones. Your best bet is to wander over to a good reference like Dinkumware and use that to figure things out. Or you could get a book on the subject. The C++ Programming Language covers standard containers in detail, and The C++ Standard Library - A Tutorial and Reference is probably the best book that covers the standard library exclusively.

This article has been dead for over six months. Start a new discussion instead.