473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

overloading [] operator for matrix?

Hi there:

i am doing a matrix class for practice. everything is ok except the []
operator. Basically, i want to achieve the same way as the normal
double[]'s, such as:
m[2][3] = 4.0;
m[3][4] = m[4][5] + m[2][3];
cout<<m[2][3]<<endl;
I also want the indices to be 1 based instead of 0 based.

One way I can think out is to have the following overloaded operator
in matrix class:
double* operator [] (int row_index); suppose the matrix contains
doubles and i am using a double pointer to store the numbers.

but I have the following problems with this approach:
1. the second index can't be 1 based since it's used to directly
indexed into the double*. It has to be 0 based.
2. it's not safe. since the function returns the row pointer. the
caller can just call m[2] and then do some bad things.

but that's the only way I can think out now. Is there a good way to
solve this problem?

Thanks a lot.
Jul 22 '05 #1
3 1681
"Dave" <da********@yahoo.com> wrote...
i am doing a matrix class for practice. everything is ok except the []
operator. Basically, i want to achieve the same way as the normal
double[]'s, such as:
m[2][3] = 4.0;
m[3][4] = m[4][5] + m[2][3];
cout<<m[2][3]<<endl;
I also want the indices to be 1 based instead of 0 based.

One way I can think out is to have the following overloaded operator
in matrix class:
double* operator [] (int row_index); suppose the matrix contains
doubles and i am using a double pointer to store the numbers.

but I have the following problems with this approach:
1. the second index can't be 1 based since it's used to directly
indexed into the double*. It has to be 0 based.
2. it's not safe. since the function returns the row pointer. the
caller can just call m[2] and then do some bad things.

but that's the only way I can think out now. Is there a good way to
solve this problem?


A good way to solve it is to have a special proxy class, which in
turn will have

double& operator[](int column)

implemented with your rules.

Make that class a member of your matrix class.

Victor
Jul 22 '05 #2
"Dave" <da********@yahoo.com> wrote in message
news:99**************************@posting.google.c om...
Hi there:

i am doing a matrix class for practice. everything is ok except the []
operator. Basically, i want to achieve the same way as the normal
double[]'s, such as:
m[2][3] = 4.0;
m[3][4] = m[4][5] + m[2][3];
cout<<m[2][3]<<endl;
I also want the indices to be 1 based instead of 0 based.

One way I can think out is to have the following overloaded operator
in matrix class:
double* operator [] (int row_index); suppose the matrix contains
doubles and i am using a double pointer to store the numbers.

but I have the following problems with this approach:
1. the second index can't be 1 based since it's used to directly
indexed into the double*. It has to be 0 based.
Why? Can't you make the pointer point one location before the actual data?
We did it for years in Fortran. Of course there is some inefficiency with
computing the offset, but that's 1-based indexing for you.
2. it's not safe. since the function returns the row pointer. the
caller can just call m[2] and then do some bad things.
Don't look now but indexing isn't safe. It's just pointer arithmetic in
disguise.

but that's the only way I can think out now. Is there a good way to
solve this problem?
You could have your operator [] return a "row" class representing a row in
your matrix. There might be some advantage to this for implementing
operations such as row swapping. However it's not going to be more efficient
for indexing than just returning a pointer and may be considerably less so.

Thanks a lot.


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3

"Dave" <da********@yahoo.com> wrote in message
news:99**************************@posting.google.c om...
Hi there:

i am doing a matrix class for practice. everything is ok except the []
operator. Basically, i want to achieve the same way as the normal
double[]'s, such as:
m[2][3] = 4.0;
m[3][4] = m[4][5] + m[2][3];
cout<<m[2][3]<<endl;
I also want the indices to be 1 based instead of 0 based.

One way I can think out is to have the following overloaded operator
in matrix class:
double* operator [] (int row_index); suppose the matrix contains
doubles and i am using a double pointer to store the numbers.

but I have the following problems with this approach:
1. the second index can't be 1 based since it's used to directly
indexed into the double*. It has to be 0 based.
2. it's not safe. since the function returns the row pointer. the
caller can just call m[2] and then do some bad things.

but that's the only way I can think out now. Is there a good way to
solve this problem?


Yes, this gets asked quite a lot. Search this group for "proxy class".

john
Jul 22 '05 #4

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

Similar topics

11
by: Andre | last post by:
Hi, I have a class which contains a two-dimensional array. I need to overload something like the operator so that I'm able to access the internal array elements via the class object. For...
3
by: A | last post by:
Hi, I understand that operator loading is used to support user-defined types. One of the requirements is that one of the arguments in an operator overloaded function must be of a user defined...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
2
by: Steffen | last post by:
Hi, is there a simple way to use operators overloaded in some class for objects of a derived class? In my example I have a class Matrix that represents 2x2 matrices, and I overloaded...
1
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
16
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...
1
by: xkenneth | last post by:
Hi, I'm writing a sparse matrix class for class and I cannot seem to get operator overloading to work properly. I've overloaded an operator with the code here. matrix operator +(matrix one,...
3
by: meLlamanJefe | last post by:
Hello, I have a short matrix class I am trying to set up for a small project. I would like to overload the + operator to simplify the code syntax. However I am running into a compilation issue...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.