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

// 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...smthing like this...

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 :

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

Its not necessary to pass those pointers by reference because the functions aren't changing them. Passing by reference in those functions does nothing more than bloat the code. Just pass the pointers instead.

>>++mat->element;
You don't want to do that. Do it like this:

for (int j = 0; j < mat->columns; ++j)
{
        print << mat->element[j] << "  ";
}

okkk...but i have one doubt...if i receive through a pointer i guess it wont create a copy...
and 2nd thing is i if i pass a pointer then i will have to receive it by a double pointer...else the change wont take place in the original pointer variable...
pls throw some light on this..

>> i if i pass a pointer then i will have to receive it by a double pointer...else the change wont take place in the original pointer variable...

No, it doesn't work that way. You don't need to change the pointer itself, only the array inside the class. I know below isn't exactly how you are using it but illustrates how to use a pointer object. Note that function foo() is not changing the value of the pointer so there is no need to pass the array pointer by reference.

int foo( int* array)
{
   for(i = 0; i < 10; i++)
      array[i] = i;
}

int main()
{
    int array[10] = {0}
    foo(array);
    cout << array[0] << "\n";
    return 0;
}

In this example, the array pointer DOES need to be passed by reference because function foo() is changing it

int foo(int **array)
{
    *array = new int[10];
    for(int i = 0; i < 10; i++)
       *array[i] = i;
   return 10;
}

int main()
{
   int *array = 0;
   foo( &array );
}

k....got your point ... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.