tags:

views:

809

answers:

7

The question could be subjective, so the syntax of

std::ostream& operator << (std::ostream & o, const SomeClass &a) {
    return o << a.accessor().. ; 
}

When do you normally define this for the classes that you write, when do you avoid writing this friend function for your class.

+1  A: 

I had never ever overloaded this one in production code. Although you might want do this if you log a lot, it'll be useful.

vava
+4  A: 

I would only overload operator<< when that has anything to do with streaming, or with shifting and the class is purely numeral. For writing something into an ostream, as in your code, i think it's fine. Anything else, i think, will cause confusion and i would better use member functions for those other purposes. One other application, which i think i would still do as an exception to the rule, is doing something like this:

StringList list;
list << "foo" << "bar" << "baz";

It is how Qt does it with its string list, and which i find quite nice.

Johannes Schaub - litb
Wow, this is nice.
Comptrol
+6  A: 

IF I want to stream a class I normally write this:

std::ostream& operator << (std::ostream& o, const SomeClass& a)
{
    a.print(o);
    return o; 
}

Then make print a const method on SomeClass that knows how to serialize the class to a stream.

Martin York
Ferruccio
a humble addition: print(ostream e.g cout<<foo1<<foo2;
Comptrol
Martin York
+3  A: 

A benefit of Martin's answer above is that you also get polymorphism for free. If you make print(ostream&) a virtual function, then the << operator acts like a virtual function as well!

As to when to overload the operator, do so anytime you think the class should be able to be written to a stream (file, socket, etc...). This might even be only for debug purposes. It is often useful to be able to output the internals of a class, so there is no real downside to overloading this operator.

Tanton Gibbs
A: 

I implement this if, and only if, I intend to make use of that operator. This is pretty much never... my reasons for not implementing it are therefore not using it. If its for public use then include it for completeness, but certainly I wouldn't include this in every class in a project of your own, since for the most part you will not need to output a class to a stream. e.g. If you wrap your entry point in a class, providing this operator will be pointless.

jheriko
+2  A: 

I would consider using it for something like logging. So you can do:

SystemLog systemLog;
systemLog << "Processing Item #15";
systemLog << "Error 0014: Bad things happened.";
systemLog << system.executeCommand(cmd); // returns a result string

Or maybe for networking as in:

NetworkInterface networkInterface;
string message("Hello World! I'm a server.");
networkInterface << message;

Of course implementing these things as regular function is also possible and might just be preferable. Generally, you should beware of operator overloading. Only use it when it really fits.

drby
A: 

I do it very frequently since it makes dumping the object to a log for debugging really convenient.

Rob K