468,458 Members | 1,846 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

cin, cout, matrix, pointers and references :(

i am working with a matrix manipulation program...consists of a matrix class and its member functions..
i have also overloaded << and >>...so dat dey can read and print d whole matrix at one statement..
the code of these overloaded operators is something like this..

Expand|Select|Wrap|Line Numbers
  1. // for >> (cin) :
  2. istream& operator >> (istream &read, matrix &mat)
  3. {
  4.     for (int i = 0; i < mat.rows * mat.columns; ++i)
  5.         read >> *(mat.element+i);
  6.     return read;
  7. }
  8.  
  9. // for << (cout) : 
  10. ostream& operator << (ostream &print, matrix &mat)
  11. {
  12.     for (int i = 0; i < mat.rows; ++i)
  13.     {
  14.         for (int j = 0; j < mat.columns; ++j)
  15.         {
  16.             print << *mat.element << "  ";
  17.             ++mat.element;
  18.         }
  19.         print << endl;
  20.     }
  21.     mat.element -= mat.rows * mat.columns;
  22.     return print;
  23. }
here i have received the variable matrix1 by reference...in main() i have used a variable matrix1 which is passed as follows :
matrix matrix1;
cin >> matrix1;

now...my question is :
wat if i want to pass a pointer variable which should be received by a reference...smthing like this...

Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.    matrix *matrix2;
  4.    cin >> matrix2; //i aint sure that this statement is right...it may be cin >> *matrix2...
  5. }
the overloaded function for pointer thing is coded as follows :

Expand|Select|Wrap|Line Numbers
  1. for >> :
  2. istream& operator >> (istream &read, matrix *&mat)
  3. {
  4.     for (int i = 0; i < mat->rows * mat->columns; ++i)
  5.         read >> *(mat->element+i);
  6.     return read;
  7. }
  8.  
  9. // for << :
  10. ostream& operator << (ostream &print, matrix *&mat)
  11. {
  12.  
  13.     for (int i = 0; i < mat->rows; ++i)
  14.     {
  15.         for (int j = 0; j < mat->columns; ++j)
  16.         {
  17.             print << *mat->element << "  ";
  18.             ++mat->element;
  19.         }
  20.         print << endl;
  21.     }
  22.     mat->element -= mat->rows * mat->columns;
  23.     return print;
  24. }

however my problem is when i execute the program...then while taking input for a pointer matrix...it just reads one value instead of all the values..but doesnt even print that single value..
i think there must be bug in those overloaded functions used for pointer variable.. :(
so please check out the functions and post the solution or your suggestions...
Mar 20 '08 #1
2 2260
weaknessforcats
9,207 Expert Mod 8TB
Passing a pointer variable by reference is the same as passing a matrix**. The only difference is that you don't have to dereference twice to get to the object. With a reference to a pointer you need dereference only once.

The larger question is why you want to pass by pointer in the first place. In C++ you pass by pointer when the function needs to chage the address in the pointer. But if your function never changes the value on the pointer but only uses it, then you should be passing by reference.

Your example inserter and extractor don't fit the model for a pointer argument. I would get rid of those funcitons.
Mar 20 '08 #2
Passing a pointer variable by reference is the same as passing a matrix**. The only difference is that you don't have to dereference twice to get to the object. With a reference to a pointer you need dereference only once.

The larger question is why you want to pass by pointer in the first place. In C++ you pass by pointer when the function needs to chage the address in the pointer. But if your function never changes the value on the pointer but only uses it, then you should be passing by reference.

Your example inserter and extractor don't fit the model for a pointer argument. I would get rid of those funcitons.


heyyy yaaa... i got it...
i was stupid to do that... :)
thnxx anyways ... :)
Mar 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by George | last post: by
3 posts views Thread by Maksim | last post: by
8 posts views Thread by wojtas.wtk | last post: by
3 posts views Thread by repairman2003 | last post: by
1 post views Thread by Sozos | last post: by
reply views Thread by Robert Latest | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.