If I had a simple class with two variables, x and y, and a function ToString() that returns a formatted string with the data. When I call
cout << simpleClass << "\n";
anyone know a way I could have simpleClass.ToString automatically called to return the correctly formatted string? I'm guessing there's a way to do this with operator functions, but I don't know how I would do this.
If you're asking how to define such an operator,
template<class CharT, class TraitsT>
std::basic_ostream<CharT, TraitsT>&
operator <<(std::basic_ostream<CharT, TraitsT>& os, SimpleClass const& sc)
{
return os << sc.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With