P
Protazy
Dear All,
I have the following piece of code:
---------------------------------------------
void f(const std:stream &str)
{
std:stringstream *pStr = (std:stringstream*)(&str);
std::cout << pStr->str();
}
int main(int argc, char** argv)
{
//This works as expected
std:stringstream str;
f(str << "Working example.\n"); //(1)
//And this prints some numbers (address of the pointer...)
f(std:stringstream() << "This is not working\n"); //(2)
std::cout << std::endl;//new line
const char* const test = "TestVariable";
//this is the address of test:
printf("The address is: %p\n",test);
//and here we have the address again
f(std:stringstream() << test);
std::cout << std::endl;//new line
//the address of test AND "some text" (?)
f(std:stringstream() << test << "some text");//(3)
std::cout << std::endl;//new line
//and again, it works this way
std:stringstream str2;
f(str2 << test);
}
-----------------------------------------------------
This is the output:
************************
Working example. <---(1)
0x446012 <---(2)
The address is: 0x446027
0x446027
0x446027some text <---(3)
TestVariable
************************
Can someone give me explanation why it is working for (1), and it is not
working for (2)?
Correct me if I'm wrong, but I thought that the bahaviour here would be
identical...
Also, the situation in (3) is strange for me.
I used cygwin (gcc 3.4.4) and Visual C++ 2003, for both compilers it looks
similar.
Thank you in advance for any help!
Best regards,
Protazy
I have the following piece of code:
---------------------------------------------
void f(const std:stream &str)
{
std:stringstream *pStr = (std:stringstream*)(&str);
std::cout << pStr->str();
}
int main(int argc, char** argv)
{
//This works as expected
std:stringstream str;
f(str << "Working example.\n"); //(1)
//And this prints some numbers (address of the pointer...)
f(std:stringstream() << "This is not working\n"); //(2)
std::cout << std::endl;//new line
const char* const test = "TestVariable";
//this is the address of test:
printf("The address is: %p\n",test);
//and here we have the address again
f(std:stringstream() << test);
std::cout << std::endl;//new line
//the address of test AND "some text" (?)
f(std:stringstream() << test << "some text");//(3)
std::cout << std::endl;//new line
//and again, it works this way
std:stringstream str2;
f(str2 << test);
}
-----------------------------------------------------
This is the output:
************************
Working example. <---(1)
0x446012 <---(2)
The address is: 0x446027
0x446027
0x446027some text <---(3)
TestVariable
************************
Can someone give me explanation why it is working for (1), and it is not
working for (2)?
Correct me if I'm wrong, but I thought that the bahaviour here would be
identical...
Also, the situation in (3) is strange for me.
I used cygwin (gcc 3.4.4) and Visual C++ 2003, for both compilers it looks
similar.
Thank you in advance for any help!
Best regards,
Protazy