+ 1

overloading operators << >>

https://www.sololearn.com/en/compiler-playground/cLxlhVfBzs32 I wrote a 2d vector class such that it has the operations += *= - *= ++v ++v , I found how the << >> can be written yet still trying to understand the code for this I assume I am allowing the function itself to access my class member v so I am befriending the operator itself not the ostream or istream class itself the type I am getting is a reference to an object of the class ostream ? is cout a named object inside the library ? std::cout<<v; such that I am inserting the result to the output stream object using << friend std::ostream& operator<<(std::ostream& s, const Vector2D& v) { return s << v[0] << ' ' << v[1]; } friend std::istream& operator>>(std::istream& s, Vector2D& v) { return s >> v[0] >> v[1]; }

17th Sep 2025, 1:15 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
3 Antwoorden
+ 3
Yes, you’re befriending the operator function, not ostream/istream. std::cout is a global std::ostream object provided by the standard library. Also returning std::ostream& (or istream&) lets you chain operations like std::cout << v1 << v2;. Inside your operator, you just forward to the stream’s own <</>> overloads for built-in types. Good luck!
17th Sep 2025, 3:45 PM
Riyadh JS
Riyadh JS - avatar
0
Thank you, Dareen Moughrabi for asking that question! 😊 Disclaimer: I'm not a C++ expert, so you can tell me if my answer is incorrect and help us to learn here. 🤓 Based on your question, let me recall that when we use the "friend" keyword in front of an operator inside a class (based on your code), it can access the private members of your Vector2D class (like coords_). It's like giving a special key to a trusted function! 🔑 Also, just to make this part clearer: when you declare friend std::ostream& operator<<(...), you are befriending the operator function itself, not the ostream or istream classes. In other words, the friend declaration means operator<< is allowed to peek inside Vector2D. You’re not modifying ostream or istream; you’re just letting this one function see Vector2D’s internals. std::cout is a pre-defined object of type std::ostream that represents the standard output stream (your console). This also applies to std::cin, which is an object of type std::istream for standard input. 💻 When you write std::cout << v;, you're calling the friend function operator<<. The compiler automatically passes std::cout as the first argument (s) and your vector v as the second. The function then inserts the vector's data into the stream object. And about the return type: the function returns a reference to the same std::ostream object you passed in (like std::cout), so the same stream keeps being used. That’s what makes chaining possible, like this: std::cout << "The value of v is: " << v << std::endl; Each << call hands back the same stream reference, so the next << can keep writing into it. ⛓️ I hope this answer helps, and thank you for making me learn something new. Have a nice day! ✨
17th Sep 2025, 2:49 PM
Seinia
Seinia - avatar
0
Riyadh JS thanks especially the part about the global object defined in the library , since when i first learnt to print it was explained as calling a function from the library to print so had to recheck the guess about it being an object known in the library all in all really nicely written thank you
17th Sep 2025, 4:02 PM
Dareen Moughrabi
Dareen Moughrabi - avatar