Consider the context, where i want to write to another file descriptor than stdout. Here, My "write" method prints to stdout, but only for understanding.
THIS IS THE MAIN PROBLEM I AM TRYING TO SOLVE:
I have a write(char*) method, i want to convert to c++ style stream through operator<<, to be able to write in C++, obfuscating multiple explicit calls of write, allowing to write in a single line of code without having to concatenate or any other solution.
PERIOD.
Though, on internet, there is nowhere to find an example that overloads operator<< with anything else than ostream on left operand. does that mean, my following example (which compiles! i tested it on a codepen-like website) is a rare case of working overload without ostream, or did i misconcepted the whole thing and "Ter" should be an ostream by inheritance ?
EDIT: How can this be a duplicate ? I am legitimately intrigued why both the topic you linked, and https://arne-mertz.de/2015/01/operator-overloading-common-practice/ or many others never talk about a left operand from another type than ostream.
EDIT2: I am having trouble to be precise on my question. Is the following code a good practice, if not, what should i do to solve the problem i braced with capital letters. Thank you for your answers
#include <iostream>
struct Ter {
void write(const std::string &str) { std::cout << str; }; //Here we should do something else in our context
friend Ter &operator<<(Ter &a, std::string &b);
};
Ter &operator<<(Ter &a, const std::string &b)
{
a.write(b);
return (a);
}
int main()
{
Ter toto;
toto << "b" << "c";
}