944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 19231
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 29th, 2004
0

overloading [] with 2 dimensional arrays

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broken_recode is offline Offline
7 posts
since Dec 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

>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:
C++ Syntax (Toggle Plain Text)
  1. // Pseudocode
  2. template <typename T>
  3. class helper {
  4. public:
  5. const T operator[](int j) const;
  6. T& operator[](int j);
  7. };
  8.  
  9. template <typename T>
  10. class matrix {
  11. public:
  12. const helper operator[](int i) const;
  13. helper& operator[](int i);
  14. };
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

//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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broken_recode is offline Offline
7 posts
since Dec 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

C++ Syntax (Toggle Plain Text)
  1. assert(0 <= subscript && subscript <= size)
If you're going to use assert, 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-lit....html#faq-13.8
Last edited by Dave Sinkula; Dec 29th, 2004 at 4:48 pm. Reason: Added link.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

Yeah, that's right! Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broken_recode is offline Offline
7 posts
since Dec 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

Quote originally posted by Narue ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broken_recode is offline Offline
7 posts
since Dec 2004
Dec 29th, 2004
0

Re: overloading [] with 2 dimensional arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 30th, 2004
0

Re: overloading [] with 2 dimensional arrays

Quote originally posted by Narue ...
"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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broken_recode is offline Offline
7 posts
since Dec 2004
Dec 30th, 2004
0

Re: overloading [] with 2 dimensional arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: adding data into an char array
Next Thread in C++ Forum Timeline: memory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC