cin, cout, matrix, pointers and references :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Encrypted
    New Member
    • Mar 2008
    • 8

    cin, cout, matrix, pointers and references :(

    i am working with a matrix manipulation program...consi sts 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..

    Code:
    // for >> (cin) :
    istream& operator >> (istream &read, matrix &mat)
    {
    	for (int i = 0; i < mat.rows * mat.columns; ++i)
    		read >> *(mat.element+i);
    	return read;
    }
    
    // for << (cout) : 
    ostream& operator << (ostream &print, matrix &mat)
    {
    	for (int i = 0; i < mat.rows; ++i)
    	{
    		for (int j = 0; j < mat.columns; ++j)
    		{
    			print << *mat.element << "  ";
    			++mat.element;
    		}
    		print << endl;
    	}
    	mat.element -= mat.rows * mat.columns;
    	return print;
    }
    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...smt hing like this...

    Code:
    main()
    {
       matrix *matrix2;
       cin >> matrix2; //i aint sure that this statement is right...it may be cin >> *matrix2...
    }
    the overloaded function for pointer thing is coded as follows :

    Code:
    for >> :
    istream& operator >> (istream &read, matrix *&mat)
    {
    	for (int i = 0; i < mat->rows * mat->columns; ++i)
    		read >> *(mat->element+i);
    	return read;
    }
    
    // for << :
    ostream& operator << (ostream &print, matrix *&mat)
    {
    
    	for (int i = 0; i < mat->rows; ++i)
    	{
    		for (int j = 0; j < mat->columns; ++j)
    		{
    			print << *mat->element << "  ";
    			++mat->element;
    		}
    		print << endl;
    	}
    	mat->element -= mat->rows * mat->columns;
    	return print;
    }

    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...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #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.

    Comment

    • Encrypted
      New Member
      • Mar 2008
      • 8

      #3
      Originally posted by weaknessforcats
      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 ... :)

      Comment

      Working...