By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

cin, cout, matrix, pointers and references :(

P: 8
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
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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

P: 8
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.