473,589 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Jul 22 '05 #1
5 3792

"Jason" <ni********@yah oo.com> wrote in message
news:de******** *************** ***@posting.goo gle.com...
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.
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::operato r() must return something for which another operator() is
valid. In other words Matrix::operato r() 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
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
Thanks for your help. I got my overloaded operators to work.
Jul 22 '05 #4

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c0******** *****@ID-196037.news.uni-berlin.de...

"Jason" <ni********@yah oo.com> wrote in message
news:de******** *************** ***@posting.goo gle.com...
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.
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::operato r() must return something for which another operator()

is valid. In other words Matrix::operato r() 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


Thanks. I got them to work from your advice.

The proxy classes are not too convenient if I want to overload the ()
operator for a 10-D array. I would have to define 9 additional proxy
classes:

double &Proxy9::operat or()(int i);
Proxy9 Proxy8::operato r()(int i);
Proxy8 Proxy7::operato r()(int i);
....
....
Proxy1 Matrix::operato r()(int i);

Is there a better way to do it? I know a 10-D array is a bit extreme, but
just want to learn :)
Jul 22 '05 #5
>
Thanks. I got them to work from your advice.

The proxy classes are not too convenient if I want to overload the ()
operator for a 10-D array. I would have to define 9 additional proxy
classes:

double &Proxy9::operat or()(int i);
Proxy9 Proxy8::operato r()(int i);
Proxy8 Proxy7::operato r()(int i);
...
...
Proxy1 Matrix::operato r()(int i);

Is there a better way to do it? I know a 10-D array is a bit extreme, but
just want to learn :)


Have a look at the boost multi_array class

http://www.boost.org/libs/multi_array/doc/index.html

which shows a completely different approach using templates.

john
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2019
by: Zenon | last post by:
Folks, I have been trying for a week but I cannot debug the following error: Error E2285 ex5.cpp 141: Could not find a match for 'matrix<complex<double>>::operator =(complex<double>)' in function main() Here is the relevant portion of my template class //***************************************************************************
10
4939
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than the column order of the datatable). I also allow the user to click on a column header to sort the datagrid by that column. I need to identify the row and column in the datatable when the user clicks on a cell in the datagrid. Using the...
0
2710
by: Leon | last post by:
Hi, I cannot solve the "400" error on the line "ActiveWorkbook.ActiveSheet.Cells(row,titleCell).Text. Please help. Thanks a lot. Best '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
16
2493
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row and _col which are respectively the array to store the data in, the number of rows and the number of columns. _m is of type T, and is size _row * _col * sizeof (T). I've managed to overload the subscript operator like this:
1
4250
by: blazted | last post by:
I am trying to find our how I would compare two matrix's. I have two matrix and I want to find out if they are equal, less or greater than. I created a double loop to loop through and compare an individual element. for(int row = 0; row < Number; row++) { for (int col = 0; col < Number; col++) {
6
3522
by: guna6680 | last post by:
i am having a matrix in the form 12345 678910 1112131415 1617181920 2122232425 what i have tried void printIt()
18
2481
by: sksriharsha | last post by:
Hi, I am finding difficulty in overloading '++' operator. I use Dev C++ compiler and I am trying it with Matrix user defined type. All other overloads are functional except "A++" where A is type Matrix. Even "++A" is functioning fine. As per me the same description should work for "A++" also. Any suggestions?? /*This is the overload Declaration for op ++*/ public: void operator ++();
4
2392
by: ShutterMan | last post by:
I have a JSON object as below (data is from SQL Server Northwind Database). But doing an eval on it returns an error "unterminated string constant" or such. Can someone help me pinpoint the element that is causing the grief? Sorry to post the entire object, but Im hoping someone has a parse utility that will help find whats wrong. { "TABLE":}, { "COL":"},{"DATA":"Education includes a BA in psychology from Colorado State University in...
1
3773
by: jr.freester | last post by:
I've written a Matrix container class and overloaded the function call operator to return values at a specified index. Below is the member function double operator()(int a , int b) { if((a < 1) || (b < 1) || ((a + 1) this->row) || ((b + 1) this- { cerr << "Invalid index for Matrix" <<endl; }
0
7865
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8224
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5399
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3889
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2374
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1461
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1198
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.