I
iu2
Hi all, can someone help me with this?
I'm trying to shorthen some logging function in our project, i.e.,
instead of
LogString("...);
use a more convenient
log() << "String here " << 12.33 << " and a number";
I create this syntax like this:
struct log : public ostringstream {
virtual ~log() { cout << "The log contains: " << str().c_str() <<
endl;}
};
The trick I'm trying to do is that log() creates an object of type
log, then it fills it using the ostringstream's parent '<<' and right
after the statement finishes the destructor is called and the message
is printed onto the screen.
The problem is that it doesn't:
It prints something like "The log contains: " 4129000
But if the first element after "log() << " is not a string, e.g., a
number, dec, hex or whatever, the scheme succeeds.
Why does this happen? I've got a feeling that the destructor might be
called before the first "<< " but I don't understand why.
Thanks
I'm trying to shorthen some logging function in our project, i.e.,
instead of
LogString("...);
use a more convenient
log() << "String here " << 12.33 << " and a number";
I create this syntax like this:
struct log : public ostringstream {
virtual ~log() { cout << "The log contains: " << str().c_str() <<
endl;}
};
The trick I'm trying to do is that log() creates an object of type
log, then it fills it using the ostringstream's parent '<<' and right
after the statement finishes the destructor is called and the message
is printed onto the screen.
The problem is that it doesn't:
It prints something like "The log contains: " 4129000
But if the first element after "log() << " is not a string, e.g., a
number, dec, hex or whatever, the scheme succeeds.
Why does this happen? I've got a feeling that the destructor might be
called before the first "<< " but I don't understand why.
Thanks