449,423 Members | 1,327 Online Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

# Help on operator overload for something like matrix(row)(col)

 P: n/a Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix { private: int row, int col; double *array_d; public: ~Matirx() Matrix(int r, int col); // array_d[row * col] double operator()(int r, int c); // matrix(2,2) = array_d[some_index] }; Here is where I get stuck: 1. So far my class can handle something like: doube temp = matrix(2,3) But I want the reverse to work: matrix(2,3) = temp; I try to expand the my operator function: matrix(i,j) = temp => matrix(i,j).operator=(temp) => matrix.operator(i,j).operator=(temp) but matrix.operator(i,j) = a double value so [double].operator=(temp) Do I overload my '=' operator to be something like this: friend operator=( double val_a, double val_b) I don't think that's right. 2. My second problem is that I want matrix(i)(j) syntax instead of matrix(i,j) to return a double. I am think matrix(i)(j) should be allowed because we can use things like any_2d_array[i][j]. I expand my function (I don't think it's right): temp = matrix(i)(j) => temp = matrix.operator()(i, matrix::operator()(j)) or => operator().(matrix(i), j) => operator().( matrix.operator()(i), j) friend double operator()( matrix::operator()(i), j) => friend double operator()( int (Matrix::*ptr)(int i), j) ----End of questions--- Here's the code I have so far: //START include using namespace std; class Matrix { private: int row, col; double *array_d; int (Matrix::*ptr)(int c);//function pointer for operator()(int c) public: Matrix(){ array_d = NULL; } ~Matrix(){ delete [] array_d; } Matrix(int r, int c) { row = r; col = c; array_d = new double[row * col]; int i = 0; for(i = 0; i I want matrix(2)(3) double operator()(int r, int c) { if((r * col + c) < row*col) return array_d[r * col + c]; else return 0.0; } //I want something like matrix(3)(4) to return a double /* matrix(i)(j) => operator()(matrix::operator()(i), j) friend double operator()(int (*ptr)(int c), int d) { cout<<"I got to here"<
5 Replies

 P: n/a "Jason" wrote in message news:de**************************@posting.google.c om... Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix { private: int row, int col; double *array_d; public: ~Matirx() Matrix(int r, int col); // array_d[row * col] double operator()(int r, int c); // matrix(2,2) = array_d[some_index] }; Here is where I get stuck: 1. So far my class can handle something like: doube temp = matrix(2,3) But I want the reverse to work: matrix(2,3) = temp; I try to expand the my operator function: matrix(i,j) = temp => matrix(i,j).operator=(temp) => matrix.operator(i,j).operator=(temp) but matrix.operator(i,j) = a double value so [double].operator=(temp) Do I overload my '=' operator to be something like this: friend operator=( double val_a, double val_b) I don't think that's right. No, you have to define double& operator()(int r, int c); because you return a reference, you can use on the lhs of assignment. 2. My second problem is that I want matrix(i)(j) syntax instead of matrix(i,j) to return a double. I am think matrix(i)(j) should be allowed because we can use things like any_2d_array[i][j]. I expand my function (I don't think it's right): Now matrix(i)(j) is the same as matrix.operator(i).operator(j) So Matrix::operator() must return something for which another operator() is valid. In other words Matrix::operator() must return a class which also has operator() defined. Classes like this are called proxy classes. Like this class Proxy { double& operator()(int j); // returns a reference }; class Matrix { Proxy operator()(int i); // returns a proxy }; I'll leave you too work out the details, will be a good learning exercise. john Jul 22 '05 #2

 P: n/a Jason wrote: Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix { private: int row, int col; double *array_d; public: ~Matirx() Matrix(int r, int col); // array_d[row * col] double operator()(int r, int c); // matrix(2,2) = array_d[some_index] Should be: double& operator()(int r, int c); double operator()(int r, int c) const; }; Jul 22 '05 #3

 P: n/a Thanks for your help. I got my overloaded operators to work. Jul 22 '05 #4 