#include <iostream>
#include <sstream>
class Logger : public std::ostringstream
{
public:
~Logger()
{
std::cout << this->str() << std::endl;
}
Logger& stream()
{
return *this;
}
};
int main( int argc, char ** argv )
{
// 1.
// Prints an address, e.g. 0x106e89d5c.
Logger() << "foo";
// 2.
// Works as expected.
Logger().stream() << "foo";
// What is the difference between 1. and 2.?
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KCmNsYXNzIExvZ2dlciA6IHB1YmxpYyBzdGQ6Om9zdHJpbmdzdHJlYW0KewpwdWJsaWM6Cgl+TG9nZ2VyKCkKCXsKCQlzdGQ6OmNvdXQgPDwgdGhpcy0+c3RyKCkgPDwgc3RkOjplbmRsOwoJfQoKCUxvZ2dlciYgc3RyZWFtKCkKCXsKCQlyZXR1cm4gKnRoaXM7Cgl9Cn07CgppbnQgbWFpbiggaW50IGFyZ2MsIGNoYXIgKiogYXJndiApCnsKCS8vIDEuIAoJLy8gUHJpbnRzIGFuIGFkZHJlc3MsIGUuZy4gMHgxMDZlODlkNWMuCglMb2dnZXIoKSA8PCAiZm9vIjsKCgkvLyAyLiAKCS8vIFdvcmtzIGFzIGV4cGVjdGVkLgoJTG9nZ2VyKCkuc3RyZWFtKCkgPDwgImZvbyI7CgoJLy8gV2hhdCBpcyB0aGUgZGlmZmVyZW5jZSBiZXR3ZWVuIDEuIGFuZCAyLj8KCglyZXR1cm4gMDsKfQo=