Standard way to implement plain text and binary input/output methods
-
I need to implement a class that offers a client two ways to output data, depending on if they want to display it in ASCII or store it in binary. I figured the best way to implement the ASCII output would be to use a friend function as follows:
friend ostream& operator <<(ostream&, myClass const&);
However, to offer a binary output option, I don't know whether I should create a friend function or a class method, and was wondering if there is an accepted best practice in such a scenario. The two options I am considering are:
friend ostream& write(myClass const&); // Option 1
void write(ostream&) const; // Option 2If either of these methods (or some other method) is the preferred way of offering such functionality, I would be interested to hear about it. Similarly, I need to implement the binary input method, for which I have the following three conceivable options:
friend istream& read(myClass&); // Option 1
void read(ostream&); // Option 2
static void read(myClass *&, ostream&); // Option 3Thanks for the help,
Sounds like somebody's got a case of the Mondays -Jeff