Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Steaming output question for C++

Status
Not open for further replies.

pghsteelers

Technical User
Apr 21, 2006
121
US
I understand how "cout" works and can show in a simple statement such as:

char name = 'Tom';
cout << "My name is " << name;

And it will out put "My name is Tom" to the screen.

But can someone explain how the follow statement actually carries the variable through the stream output such as:

#include <iomanip>
char capital = 'A'
cout << hex << static_cast<int>(capital);

You can bypass explaining the static_cast as I know about that, I just like to know the "Why's" something happens to grasp everything about it. And applying the same operation as I see the first example, doesn't show me how the program knows that it is syphoning the variable "capital" through the manipulator "hex".

What am I missing that makes that statement be read differently than the first example and not want to place to variables (hex and capital) as output to the screens?
What if "hex" was changed out with something else like an expression (capital + 'B').

Can anyone clarify how I should be seeing the second example different that the first? or how I shoud read it?
 
I left out part of the second example, should be:

cout << capital << hex << static_cast<int>(capital);

and the output is

A 41

 
Is it the declaration of the directive #iomanip and then seeing the namespace "hex" that tells the program what to do?
 
What!? You mean you can't figure out what a simple function like this does?
Code:
inline ios_base& __CLRCALL_OR_CDECL hex(ios_base& _Iosbase)
	{	// set basefield to hex
	_Iosbase.setf(ios_base::hex, ios_base::basefield);
	return (_Iosbase);
	}
I'd think it would be clear as mud. ;-)

Well don't quote me on this, but if I remember reading correctly... There is one static cout object in your process, and the hex modifier (function) calls the ios_base::setf() (which is a base class deep in the bowels of cout) that sets the internal state flag to cause it to display all numbers in hexidecimal.
 
The point is that manipulator is a trick with C++ operator overloading. Indeed, look at the expression:
Code:
cout << hex << 16
Yes, hex is a global function from STL. It has ostream& paremeter and returns ostream& result. This function effect is to change internal ostream object flag (see <ostream> header). But in the expression above we have a function name (impicitly converted in a pointer to a function), not this function call hex().

Don't worry, no need in a special magic. C++ STL ostream has overloaded operator << which calls this function when a pointer to this function occured in the proper context.

So you may write your own manipulator, for example:
Code:
ostream& itsme(ostream& os)
{
   os << " Hurrah, it's my own manipulator here";
   return os;
}
...
cout << itsme << "!!!" << endl;
No need in <iomanip> header for manipulators w/o parameters. But if you want to define your own manipulator with parameters (like setw(n)), you need <iomanip>. Based on the same method in principle, calling of manipulators with parameters uses slightly different mechanics. Read about this issue in the wonderful "The C++ Programming Language" by Stroustrup.

Apropos, internal ostream flag changed by hex manipulator has an effect on all subsequent ops on a stream. So don't forget to return to dec mode...
 
Sorry, add backslash in the string literal above: ...it\'s...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top