What does object.operator do?

c++

Solution

`object.operator<<(??);` is not quite the same as `std::ostream::operator<<(std::ostream&, ??)`, because it 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, but recall that you can add free `operator<<` functions as well, in addition to the 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 the `object.operator<<(??)` overload, you've told it explicitly not to use the free functions, and only use to use the member functions. The best match for `"HELLO"` was the `void*` overload, so it printed the address of the string.

Problem

I saw the following code: ``` cout.operator << ("Hello"); ``` And thought it would be the same as: ``` cout << "Hello"; ``` But it prints: ``` 0x46e030 ``` How does it work? What can it do?

Original source

Related problems