I’ve a working logger class, which outputs some text into a richtextbox (Win32, C++). Problem is, i always end up using it like this:
stringstream ss; ss << someInt << someString; debugLogger.log(ss.str());
instead, it would be much more convenient to use it like a stream as in:
debugLogger << someInt << someString;
Is there a better way than forwarding everything to an internal stringstream instance? If’d do this, when would i need to flush?
You need to implement
operator <<appropriately for your class. The general pattern looks like this:Notice that this deals with (non-
const) references since the operation modifies your logger. Also notice that you need to return thelogparameter in order for chaining to work:If the innermost operation didn’t return the current
loginstance, all other operations would either fail at compile-time (wrong method signature) or would be swallowed at run-time.