overloading [] with 2 dimensional arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 7
Reputation: broken_recode is an unknown quantity at this point 
Solved Threads: 0
broken_recode broken_recode is offline Offline
Newbie Poster

overloading [] with 2 dimensional arrays

 
0
  #1
Dec 29th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: overloading [] with 2 dimensional arrays

 
0
  #2
Dec 29th, 2004
>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:
  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. };
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 7
Reputation: broken_recode is an unknown quantity at this point 
Solved Threads: 0
broken_recode broken_recode is offline Offline
Newbie Poster

Re: overloading [] with 2 dimensional arrays

 
0
  #3
Dec 29th, 2004
//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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,459
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 252
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: overloading [] with 2 dimensional arrays

 
0
  #4
Dec 29th, 2004
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: overloading [] with 2 dimensional arrays

 
0
  #5
Dec 29th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 7
Reputation: broken_recode is an unknown quantity at this point 
Solved Threads: 0
broken_recode broken_recode is offline Offline
Newbie Poster

Re: overloading [] with 2 dimensional arrays

 
0
  #6
Dec 29th, 2004
Yeah, that's right! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 7
Reputation: broken_recode is an unknown quantity at this point 
Solved Threads: 0
broken_recode broken_recode is offline Offline
Newbie Poster

Re: overloading [] with 2 dimensional arrays

 
0
  #7
Dec 29th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: overloading [] with 2 dimensional arrays

 
0
  #8
Dec 29th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 7
Reputation: broken_recode is an unknown quantity at this point 
Solved Threads: 0
broken_recode broken_recode is offline Offline
Newbie Poster

Re: overloading [] with 2 dimensional arrays

 
0
  #9
Dec 30th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: overloading [] with 2 dimensional arrays

 
0
  #10
Dec 30th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC