This is my code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class TestLog: public std::stringstream
{
public:
~TestLog()
{
cout << (str()) << endl; // Why does this print an address ?
}
};
int main()
{
TestLog() << "Hello World!"; //test 1 print an address
stringstream ss;
ss << "Hello World!";
cout << (ss.str()) << endl; //test 2 print a string
return 0;
}
And the output:
0x401b90
Hello World!
Compiler info :
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
In my opinion,
(a)the str() method of std::stringstream return a string.
(b)std::cout is an object of std::ostream.
So both of the two test will call the same operator funtion of ostream and print same "Hello world".
But test 1 print an address , test 2 print the correct "Hello world".
What wrong with me ? Thanks.