Streams

Overloading << Operator For ostream

A common and useful practise is to overload the << operator for an ostream on your own classes. This allows you to then write things such as:

One way to do this is to overload the operator from within your class header file, declaring std::ostream as a friend:

And then the definition in the .cpp would look like:

A Generic Overload For All Classes

Since C++11, you can make a generic overload that will work for all classes that contain a public method called Print(std::ostream&)const.

This saves you to work of having to overload the << operator for each class you design!

Posted: November 11th, 2017 at 5:57 am
Last Updated on: December 2nd, 2017 at 5:36 am