Advertisement

Tricky stuff like cout

Started by September 14, 2000 08:17 PM
5 comments, last by gimp 24 years, 3 months ago
I have a game console up and running and would like to be able to use the shift operator like cout does to shove stuff in to it. But... How does this work: cout << "I''m " << inAge << "years old" << endl; is this just a matter of overloading operator << in loads of different ways to accept all the different data types. Anyone know of a good tutorial on this
Chris Brodie
Yep, just overload the << operator to take whatever data type you want. I''m not sure about a tut, check the code archives for Operator Overloading... if its not there (this is a game coding site, not general afterall), check out www.programmersheaven.com or some other coding site. Or better yet, crack open your favorite c++ book (you do have a book right?). =)
There is no spoon.
Advertisement
I believe cout either overloads many times, or uses templates(probably a better way), and recursively calls itself for every arg.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
quote: Original post by ImmaGNUman

I believe cout either overloads many times, or uses templates(probably a better way), and recursively calls itself for every arg.


Overloads the kitchen sink, cout predates templates by a number
of years, plus the implementaion is quite different for each type.

sstring is probibly going to be real helpful here. Works just like cout/cin except it writes to/reads from a string that you can access with .str()
It''s not cout that has the overloads, it''s std::ostream (which cout is a subclass of). So the best way to get the results you want is your own stream then you can use all the existing overloads. Also you can redirect cout to any other stream, so if you do that at the start of main you can use cout in the rest of your program.

I think what you need is a custom stream buffer (as opposed to a std::ostream sub class), but I can''t remember the exact details.
This looks like it''s what your look for.
Advertisement
Wow... good guessing wilka... in the back of my mind thats exactly what I was hoping for... replacing cout (once I get it working) will be _VERY_ cool. I have some server and clients processes and being able to just write all output with cout from anywhere in code will make the messaging much easier, when in console mode on a server stdout gets the data but when on my client the opengl console will get the data... very cool.

Thanks.. I''ll start looking in to that ostream now...

gimp
Chris Brodie

This topic is closed to new replies.

Advertisement