-
ostream problem...
my code requirement is that a string between two macros is to be displayed at the console. Like this:
Code:
TRACE << "hello world" << ENDTRACE;
Question is: how should the two MACROS be defined.
I am thinking of TRACE expanding to a static ostream object whose inserter can be overloaded to display the string at the console. What do you think?
Any ideas would be helpful.
TIA
-
Code:
#include <iostream.h>
#define TRACE cout
#define ENDTRACE endl
int main(void)
{
TRACE << "hello world" << ENDTRACE;
}
It's a bit poor really, but it works.
-
well, for the given problem that's probably the simplest and best solution.
But I didn't specify the problem in detail :D The reason I said console was because that was what I was trying to do in a test program.
Ok, so the problem is: I need to invoke a method passing on the string between the two MARCOS. Something like:
Code:
TRACE << "The number is: " << some_method_returning_int() << ENDTRACE;
if some_method_returning_int() returned: 5, the effect of macro expansion should be:
Code:
someObj::trace_to_correct_channel( "The number is: 5" );
-
A solution to your problem would be:
Code:
#define TRACE {stringstream __a;__a
#define ENDTRACE "";someObj::trace_to_correct_channel(__a.str().c_str());}
I do not recommend use of macros like this, though.
(Edit) Don't forget to #include <sstream>