I am writing my own integer class that can handle integers of any size. So far, I have overloaded the following operators successfully: =, +, -, *, /, %, >, <, >=, <=, ==, !=, +=, -=, *=, /=, and %=.
Now, I'm trying to overload the <<
operator to mimic the behavior of int's in the following lines of code:
int a = 5;
std::cout << a;
I have been looking at how to do this for a while, and so far all I've found is this:
std::ostream& operator<<(std::ostream& os, const T& obj)
{
// Write obj to stream
return os;
}
But this seems to be for if I wanted to stream something into my object (that is, having the <<
on the right side of my object). But I want to change the behavior of <<
when it is on the LEFT side of my object.
How can I set up the operator<<
function to allow me to stream data into cout (or any other ostream
)?