Operator overloading

Hi, I know to overload the operator[] you write
1
2
3
int & operator[](int x){
....
}


I was wondering how to do operator[][]
1
2
3
 int & operator[][](int x , int y){
...
}


I get the error: function definition does not declare parameters
Could someone explain why this does not work.

The [] operator takes only one argument. That's just the way it works.

These 2 FAQs should cover it pretty well:
http://www.parashift.com/c++-faq/matrix-subscript-op.html
http://www.parashift.com/c++-faq/matrix-array-of-array.html
I read the article but its really confusing on how to make the array[][] work instead of array()(). Can any one provide an example ?
It tells you not to try to make it work. It tells you to use array(i,j).
With brute force, you can make the subscript variant work, of course. You just need to have operator[] return an object that also provides operator[] itself.
@Athar "You just need to have operator[] return an object that also provides operator[] itself." I dont know how to do this. I read this forum page
http://v2.cplusplus.com/forum/general/8017/ but the problem is I can only have one main class.
Sorry I am new to this
Last edited on
Exactly what is a main class?
I just meant that I have main.ccp and test.h /.cpp with a private template multidimensional array and I have to be able to access it using [][] in my main.cpp. I have overloaded all the other operation such as *,+= but have no idea how to overload [][]. I know how to get the same result using operator()
Last edited on
but have no idea how to overload [][]


EDIT:
to clarify... [][] is not an operator. [] is the operator.
/EDIT

Not to sound like a broken record... but the point is that you shouldn't overload [][] because it's problematic. It splits the job of one task (dereferencing) across two separate calls (one call for each [] operator).

If you really want to make this work, you need to overload the [] operator so that it returns a type that can also have the [] operator used on it. This means:

1) return a pointer (awful because it breaks encapsulation and prevents the possibility of bound checking)

or

2) Creating a sub-class that also has the [] operator overloaded. (awful because it's excessively complicated and usually requires some interclass dependencies).


The idea is this:
1
2
3
4
5
6
7
8
mymatrixtype<int> foo;

// this:
foo[row][column] = 5;

// is really this:
mysubtype<int>& bar = foo[ row ];  // foo[] returns a subtype which gives you the row
bar[ column ] = 5;   // bar[] returns the actual data 
Last edited on
test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class Y>
class test{
public:
 Y * operator[](int rows){
   return (array[rows]);
 }

private:
 Y** array;
int ro;
int col;

};


This is what I have but I don't know how to implement the solution 1 suggest above. I have to be able to use [][] in my int main(). Sorry that I am not understanding this
Last edited on
This is what I have but I don't know how to implement the solution 1 suggest above


What you have is exactly what solution 1 is.

The only problem is you probably meant to type array and not matrix on line 5.

but again.... eewwwww
Last edited on
Thanks a lot guys. Especially Disch It turned out I had another error in the code. I know its bad programming to do this but its my assignment.
Topic archived. No new replies allowed.