views:

92

answers:

3

As in, should the operator>> match the operator<< ?

Database Example:

If the operator>> reads in something of the following format:

2
Joe 500 20 1
Bob 250 30 0

should the operator<< output that? Or something like this:

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

And then have a separate writeFile() function?

I know either would work, but what is the "accepted standard"?

+3  A: 

This kind of operator overloading is IMO a big misuse and misconception. Use overloading where it really makes some sense.

For debug purposes, have toString() and override << to call it. Do not override >> at all.

>> and << generally serve for sending serialized data to streams, not for communication with user.

My 2 eurocents.

Ondra Žižka
I disagree: if you intend to read or write data from a stream, then overloading the "stream operators" is both straightforward and idiomatic.
James McNellis
You're right - I was adding that to my answer when you were writing this :)
Ondra Žižka
+7  A: 

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

James McNellis
A: 

Both or independent of each other, according to your will you can overload them.

prabhakaran
Billy ONeal
@Billy ya, you are correct. But overriding << is always good for frequent checking(when you are coding).
prabhakaran