Question:
What does C++'s "cout" method really look like?
Marc
2010-06-08 14:01:18 UTC
For everyone who can program in c++ you know that in order to display console output you need to use the "cout <<" method.

Well in order to use that you also need to include the iostream library. If cout is the method to produce output then why do you need to include the library.

What does the output method really use to display output?

is it something like:

0xF3
getassem
output at 0x5

Thanks,

Also, how can a find these libraries so that I can see what really happens when the program comes to cout<
Three answers:
Tumbleweed
2010-06-08 18:05:37 UTC
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.
?
2010-06-08 21:07:52 UTC
"cout <<" is the text in the source code that converts to a call to the cout method in the runtime library. The library is needed to supply the actual compiled code for cout.



And it's not really the iostream library. iostream.h is the header file with the method declaration for cout so the C++ compiler can verify calls to it. The actual library is something like MSVCP81.dll.



Don't confuse what the programmer puts in the source code with the object code or executable that gets produced from it.



If you use the C++ Integrated Development Environment in Debug Mode, you can step into the cout method.



Hope that helps.
2010-06-09 06:27:37 UTC
All you're doing is defined a method of MyClass called Cout. To, use it, you would have to call it:

C++ Syntax (Toggle Plain Text)



1.

MyClass obj;

2.

obj.Cout();



MyClass obj; obj.Cout();



the cout you use to do things like cout << x; is not a function, it's an object. Look in to operator overloading, if you haven't already.



To allow you to use the insertion (shift-left) operator (<<) with left operand of ostream (the class that cout is an object of) and right operand of MyClass, do something like this (I haven't actually run this, so it may not work):


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...