How to overload << operator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to overload << operator?

I need overload << operator. For example: class myClass{...}; myClass a; cout<<a; //this must write in console all elements of char array (arr) from object a of myClass.

16th Jan 2021, 11:06 AM
Влад Цислевский
Влад Цислевский - avatar
1 Answer
+ 6
The basic signature is std::ostream& operator << ( std::ostream&, const MyClass& ); Usually, you declare this as a friend function to your class, so that it has access to the private properties of the class, although this really depends on what you want to write into the output stream. Inside the definition, you can then use the std::ostream instance the same way you use std::cout. Don't forget to return that instance at the end.
16th Jan 2021, 11:11 AM
Shadow
Shadow - avatar