473,387 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2549
weaknessforcats
9,208 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

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

Similar topics

5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
1
by: SUPER_SOCKO | last post by:
Suppose I have a matrix M x N dimension. Normally, to print all of the elements in the matrix. I write the following codes: Suppose M = 5, N = 4: for(int j=0; j<5; ++j) { for(int k=0; k<4;...
1
by: George | last post by:
I have two questions that I need help with the first when I create the array using the random function it does not print the first part of the matrix ie: Recursive Matrix Chain 0 0 0 ...
3
by: Maksim | last post by:
I'm trying to imprort an AVL Tree written in C++ to C# and I have a problem translating this statement: root_parent = (Node *) &this->_Root I'm using classes for my Nodes and so I can't use '&'...
4
by: naveid | last post by:
I have an array (List<T>) containing tens of thousands of items. I need to maintain copies of the array, sorted in a few different ways. I'm working on Windows Mobile so memory is a constraint. To...
8
by: wojtas.wtk | last post by:
How write class of matrix 2x2 acted following program in order to? int main(int argc, char **argv) { const double tab= {{1.5, 2.5}, {3.5, 4.5}}; matrix2x2 a(tab), b(1.0, 0.0, 0.0, -1.0), c =...
3
by: repairman2003 | last post by:
I'm having some trouble with poitners and dynamic arrays (a matrix). Given this function I have a few questions. void func(int* mat, int rows, int columns, char* out) { ... }
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: Robert Latest | last post by:
Gary Herron wrote: Good to know. I just wanted to make sure before writing more code which in the end might not scale well. Thanks to all for the help! robert
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.