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).ope rator=(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::operato r()(j))

or

=> operator().(mat rix(i), j)

=> operator().( matrix.operator ()(i), j)

friend double operator()( matrix::operato r()(i), j) =>

friend double operator()( int (Matrix::*ptr)( int i), j)

----End of questions---

Here's the code I have so far:

//START

include<iostrea m>

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<row*col;i++) array_d[i] = i;

ptr = &Matrix::operat or(); //assign function pointer

}

int operator()(int c)

{

return c;

}

// matrix(2,3) return a double => 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()(matr ix::operator()( i), j)

friend double operator()(int (*ptr)(int c), int d)

{

cout<<"I got to here"<<endl;

}

*/

};

int main()

{

Matrix matrix(10,10);

cout<<matrix(3, 3); //it's fine

// matrix(3,3) = 123.4;

// cout<< matrix(3)(3);

return 0;

}

//END