Operator []
I have a class CMatrix and I want to overload the [] operator to be able to access an array of int **Array; I want to be able to acess the data of the array by this way:
CMatrix matrix;
matrix[0][0] = 32;
Or something like that... But I don''t know how to overload the [] operator to acess the second dimension of my array... If you could help me with this it would solve a lot of problems... =)
Thanks!
Ha! Nice question, almost had me stumped But not quite.
okay heres how:
cool stuff since it returns a pointer to an int it works like:
Now I hope you check back to see if some one answered
Okay, It's not a perfect solution. You can't return a const int* because then you cant assign stuff, and you loose some encapsulation. But virtualy all implementations of this style have the same problem. So even though the data is private in the class it can be publicaly accessed and messed with if your not careful.
Edited by - Atavist on September 19, 2000 6:34:12 PM
okay heres how:
class matrix{ myArray[10][10]; //or a dynamic one or whateverpublic: int* operator [] (int index); //you return a pointer, thats the trick};int* matrix::operator [](int index){ return myArray[index];}int main(){ matrix matrixA; matrixA[2][3] = 5; int number = matrix[2][3]; return 0;}
cool stuff since it returns a pointer to an int it works like:
int *p = (matrixA[2]);p[3] = 5;
Now I hope you check back to see if some one answered
Okay, It's not a perfect solution. You can't return a const int* because then you cant assign stuff, and you loose some encapsulation. But virtualy all implementations of this style have the same problem. So even though the data is private in the class it can be publicaly accessed and messed with if your not careful.
Edited by - Atavist on September 19, 2000 6:34:12 PM
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Oopsie! Can't do that in C++.
But you can do this:
That's how D3D does it anyway.
Edited by - Meduzza on September 19, 2000 6:48:08 PM
But you can do this:
class CMatrix{ float m[4][4];}CMatrix matrix;matrix.m[2][3] = 5;
That's how D3D does it anyway.
Edited by - Meduzza on September 19, 2000 6:48:08 PM
Work perfectly! =)
It'll help a lot in the edition of the matrix... =) Because calling a function all the time to put data in the matrix is ugly and long to type... =)
The first method work perfectly. Didnt tried your method Meduzza but the first one work... =)
Thanks to both for your replies! =)
Edited by - Crocmort on September 19, 2000 6:51:21 PM
It'll help a lot in the edition of the matrix... =) Because calling a function all the time to put data in the matrix is ugly and long to type... =)
The first method work perfectly. Didnt tried your method Meduzza but the first one work... =)
Thanks to both for your replies! =)
Edited by - Crocmort on September 19, 2000 6:51:21 PM
September 19, 2000 05:52 PM
Don''t forget that you can return objects as well as primitive types. Keep that objects small and you don''t have a serious perf hit.
Here''s a version that lets you keep encapsulation and do other fun stuff:
Here''s a version that lets you keep encapsulation and do other fun stuff:
class Matrix{public: class Row { public: Row(int *_r) {r = _r;} int & operator[] (int i) { return r; // can do range checking, etc } private: int *r; }; Row operator [] (int i) { return Row(m);<br> }<br><br>private:<br><br> int m[10][10];<br>};<br><br>void main()<br>{<br> Matrix m;<br><br> m[2][3] = 0;<br>}<br> </pre> <br><br>-Mike </i>
September 19, 2000 05:55 PM
Umm, looks like the code tag can really screw up things. That should be return r for example, not return r. Hopefully you get the idea.
-Mike
-Mike
quote:
Grrrrr! How about r left-bracket i right-bracket.
That''s why you gotta use the "source" tags. Check out the faq to get these kinds of boxes:
x<i> = something;
September 19, 2000 06:36 PM
It looks like the code tag does not disable other nested tags. Seems like a bug to me, it''s code, not format after all.
Ok, I have an other question in the same subject... If I want to initialize my matrix with something like this:
A 2x2 matrix:
CMatrix matrix(2,2) = {{1,2},{2,1}};
(just like by declaring an array[][] where you would initialize the data in the declaration of the variable)
The data would copy at the right place in the array in the class... =) I don''t know if it can work but right now I really don''t know how to create a constructor that would do this...
Thanks for all the help everyone! =)
A 2x2 matrix:
CMatrix matrix(2,2) = {{1,2},{2,1}};
(just like by declaring an array[][] where you would initialize the data in the declaration of the variable)
The data would copy at the right place in the array in the class... =) I don''t know if it can work but right now I really don''t know how to create a constructor that would do this...
Thanks for all the help everyone! =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement