The catch is that cout is not the actual method used, but a way to tell the compiler what you want to do. The use of the cout function is outlined in the header file so that the compiler knows how to treat it when it is compiled into an object (OBJ) file. The linker can then match up the outlined code to the run-time library and pass through the parameters needed to make the call.
On a Windows machine, the actual trek of the cout call starts in the ostream module and meanders through the ios, streambuf, xmutex, xmtc, crtexe, xiosbase, xlocale and more before the characters finally pop up on the console. In order to see this, you will need to run the code under a debugger and follow the instruction pointer and stack throughout the system calls. You can get a good idea of what this looks like at a source level if you're using the Microsoft Visual Studio debugger.
For example, the code snippet "cout << Test << endl;" where Test is an integer starts off here:
_Myt& __CLR_OR_THIS_CALL operator<<(int __w64 _Val)
{ // insert an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
ios_base::fmtflags _Bfl =
ios_base::flags() & ios_base::basefield;
long _Tmp = (_Bfl == ios_base::oct
|| _Bfl == ios_base::hex)
? (long)(unsigned int)_Val : (long)_Val;
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Tmp).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
This is the first stop where it determines what to do with "<<" operator followed by an integer. Each of these modules, in turn, have already been compiled into DLLs and need only be invoked at run-time with the proper calling sequence. On most modern machines, they also will link into DLLs provided by the manufacturer of the video card (in the case of console-bound streams) or the disk sub-system (in the case of disk-bound streams), etc. A mismatched DLL will result in driver errors and compatibility issues.