c++ - What does object.operator do? -
this question has answer here:
i saw following code:
cout.operator << ("hello");
and thought same as:
cout << "hello";
but prints:
0x46e030
how work? can do?
object.operator<<(??);
not quite same std::ostream::operator<<(std::ostream&, ??)
, because excludes non-member (free) functions.
the std::ostream::operator<<
members are:
basic_ostream& operator<<( short value ); basic_ostream& operator<<( unsigned short value ); basic_ostream& operator<<( int value ); basic_ostream& operator<<( unsigned int value ); basic_ostream& operator<<( long value ); basic_ostream& operator<<( unsigned long value ); basic_ostream& operator<<( long long value ); basic_ostream& operator<<( unsigned long long value ); basic_ostream& operator<<( float value ); basic_ostream& operator<<( double value ); basic_ostream& operator<<( long double value ); basic_ostream& operator<<( bool value ); basic_ostream& operator<<( const void* value ); basic_ostream& operator<<( std::basic_streambuf<chart, traits>* sb); basic_ostream& operator<<( basic_ostream& st, std::ios_base& (*func)(std::ios_base&) ); basic_ostream& operator<<( basic_ostream& st, std::basic_ios<chart,traits>& (*func)(std::basic_ios<chart,traits>&) ); basic_ostream& operator<<( basic_ostream& st, std::basic_ostream& (*func)(std::basic_ostream&) );
the lack of const char*
might seem confusing, recall can add free operator<<
functions well, in addition above members. <ostream>
includes these:
ostream& operator<<( ostream& os, chart ch ); ostream& operator<<( ostream& os, char ch ); ostream& operator<<( ostream& os, char ch ); ostream& operator<<( ostream& os, signed char ch ); ostream& operator<<( ostream& os, unsigned char ch ); ostream& operator<<( ostream& os, const chart* s ); ostream& operator<<( ostream& os, const char* s ); ostream& operator<<( ostream& os, const char* s ); ostream& operator<<( ostream& os, const signed char* s ); ostream& operator<<( ostream& os, const unsigned char* s ); template< class t > ostream& operator<<( ostream&& os, const t& value );
by specifically calling out object.operator<<(??)
overload, you've told explicitly not use free functions, , use use member functions. best match "hello"
void*
overload, printed address of string.
Comments
Post a Comment