Re: cannot compile example 1-1 of "More Exceptional C++"
wei.niu@gmail.com wrote:
:: On 8 21 , 9 55 , Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
::: The difference is that operator<< for char* is a member while the
::: one for string is a non-member function. As such, it behaves like
::: any other function, even non-overloaded operators as in your
::: example. Now, the difference is that you can call non-const
::: memberfunctions on a temporary but not otherwise bind them to a
::: non-const reference.
::
:: Yes,you are right.Thank you!
::
::: 'out' is a reference to an ofstream, that cast only converts it
::: to a reference to an ostream - no temporaries are involved
::
:: 'out' is a reference to an ofstream?'out' is not an ofstream
:: object?
Here 'out' is an object of type ofstream.
::
:: But after reading the asm code generated by VC7.1,I found "no
:: temporaries are involved" is true.What does the compiler do when it
:: see static_cast<ostream&>( out )?
It verifies that the cast is possible. As ofstream is publicly
inheriting from ostream, it doesn't have to generate any code to do
the conversion.
A reference is just an alias for the original object.
#include <fstream>
int main()
{
std::ofstream xout("C:/temp/xout.txt");
std::ostream& x = xout;
x << "Hello world!";
}
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]