D
David Given
(No, I'm *not* trying to call a constructor from another constructor!)
I have this class:
class P : public std::stringstream
{
public:
~P() { std::cout << str(); }
};
That is, it behaves like a stringstream, but on destruct it prints
itself. This allows me to do this:
{
P p;
p << "Hello, world!";
}
....and I get 'Hello world!' on stdout.
However, if instead I do this:
P() << "Hello world!";
....I get '0x12345678' instead (that is, some random hex number).
Obviously what's happening is that it's not finding the const char*
iostream method on P, and is falling back to the void* one; but I don't
understand why. The temporary P instance is getting created and
destructed at the appropriate points, but it's behaving as if it's lost
some of its stringstream methods.
I use this idiom extensively for various printers and loggers and so on;
they all work fine. This is the first time I've tried it with a subclass
of stringstream, though; all the others have delegated to an embedded
instance of stringstream rather than subclassing it. I thought I'd save
myself some typing this way. But why doesn't it work? More importantly,
why is it not working in this really bizarre fashion?
I have this class:
class P : public std::stringstream
{
public:
~P() { std::cout << str(); }
};
That is, it behaves like a stringstream, but on destruct it prints
itself. This allows me to do this:
{
P p;
p << "Hello, world!";
}
....and I get 'Hello world!' on stdout.
However, if instead I do this:
P() << "Hello world!";
....I get '0x12345678' instead (that is, some random hex number).
Obviously what's happening is that it's not finding the const char*
iostream method on P, and is falling back to the void* one; but I don't
understand why. The temporary P instance is getting created and
destructed at the appropriate points, but it's behaving as if it's lost
some of its stringstream methods.
I use this idiom extensively for various printers and loggers and so on;
they all work fine. This is the first time I've tried it with a subclass
of stringstream, though; all the others have delegated to an embedded
instance of stringstream rather than subclassing it. I thought I'd save
myself some typing this way. But why doesn't it work? More importantly,
why is it not working in this really bizarre fashion?