Should the << operator be formatted for classes?

As if the operator → corresponds to the operator <<

Example database:

If the operator → reads something in the following format:

 2 Joe 500 20 1 Bob 250 30 0 

if the operator <<output? Or something like this:

 Record: 1/2 Name: Joe Balance: 500 Transactions: 20 Premium Account: Yes 

And then you have a separate writeFile () function?

I know that any of them will work, but what is the “accepted standard”?

+6
source share
3 answers

If you have an istream operator>> overload for a type that reads data in a specific format, then if you implement an ostream operator<< overload for the same type, it should be output in the same format (otherwise it might get confused) .

+7
source

Such operator overloading is IMO a lot of abuse and misconception. Use overload where it really makes sense.

For debugging purposes, use toString() and override <call it. Do not redefine → at all.

>> and << usually serve to send serialized data to streams, and not to communicate with the user.

My 2 euro cent.

+3
source

Both or independently, according to your will, you can overload them.

0
source

All Articles