472,780 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 2497
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.