Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why cout and cin are being passed as reference objects while overloading insertion and extraction operators?

What I have tried:

I overloaded insertion and extraction operator but in my places it is mentioned to pass it as reference.Just wanted to know why so?
Posted
Updated 29-Jul-21 18:57pm
v2

They are streams, so you want to pass them by reference & not by value. Otherwise, you make their shallow copies that don't update their original internal pointers anymore.
 
Share this answer
 
To add to what Code Fan has said, have a look at this: Using struct and class - what's that all about?[^]
It's about C#, where a class is a Reference Type and a struct is a Value Type but it explains why they are different and that applies in C++ as well.
 
Share this answer
 
Comments
Code Fan 30-Jul-21 1:28am    
C++ is different, with struct declaring public members by default & class private members by default. Other than that, struct & class are exactly the same in C++.
OriginalGriff 30-Jul-21 1:53am    
Yes I know - that's why I said about the C# difference. But ethe effect of Reference and Value types is the same in both.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900