 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave Guest
|
Posted: Sat Feb 28, 2004 5:08 am Post subject: 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<
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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Feb 28, 2004 5:32 am Post subject: Re: overloading [] operator for matrix? |
|
|
"Dave" <davis_eric (AT) yahoo (DOT) com> wrote...
| Quote: | 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<
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
|
|
| Back to top |
|
 |
Cy Edmunds Guest
|
Posted: Sat Feb 28, 2004 5:45 am Post subject: Re: overloading [] operator for matrix? |
|
|
"Dave" <davis_eric (AT) yahoo (DOT) com> wrote
| Quote: | 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<
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.
| Quote: | 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.
| Quote: |
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.
--
Cy
http://home.rochester.rr.com/cyhome/
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Feb 28, 2004 7:22 am Post subject: Re: overloading [] operator for matrix? |
|
|
"Dave" <davis_eric (AT) yahoo (DOT) com> wrote
| Quote: | 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<
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|