954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

overloading [] with 2 dimensional arrays

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?

broken_recode
Newbie Poster
7 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>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);
};
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

//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.

broken_recode
Newbie Poster
7 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
assert(0 <= subscript && subscript <= size)


If you're going to useassert, use it correctly.

assert(0 <= subscript && subscript < size);


And you may also want to take a peek at this: http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.8

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Yeah, that's right! Thanks!

broken_recode
Newbie Poster
7 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
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.

broken_recode
Newbie Poster
7 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
"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!

broken_recode
Newbie Poster
7 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

>@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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You