Overriding std::cout or std::cerr

J

Jim Langston

I have a computer game that has no console. If I want to output something I
push a string to the back of a std::vector<std::string> and that gets output
and maintained.

All is well and good, but what I would like to do is to be able to use
ostream's power for this output. I could create my own ostream and check it
every cycle or something, but then I would have to pass this ostream or make
it global.

What I would like to do is have something like:

std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
<< "\n";

and have that get redirected, somehow, to my vector.

If no one has an easy way to do this I'll have to go with the global ostream
approach and pull everything out of it every cycle.
 
R

Rolf Magnus

Jim said:
I have a computer game that has no console. If I want to output something
I push a string to the back of a std::vector<std::string> and that gets
output and maintained.

All is well and good, but what I would like to do is to be able to use
ostream's power for this output. I could create my own ostream and check
it every cycle or something, but then I would have to pass this ostream or
make it global.

What I would like to do is have something like:

std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
<< "\n";

and have that get redirected, somehow, to my vector.

Write a streambuf that appends to the vector, then replace cout's one with
that.
 
T

Thomas J. Gritzan

Jim said:
I have a computer game that has no console. If I want to output something I
push a string to the back of a std::vector<std::string> and that gets output
and maintained.

All is well and good, but what I would like to do is to be able to use
ostream's power for this output. I could create my own ostream and check it
every cycle or something, but then I would have to pass this ostream or make
it global.

What I would like to do is have something like:

std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
<< "\n";

and have that get redirected, somehow, to my vector.

If no one has an easy way to do this I'll have to go with the global ostream
approach and pull everything out of it every cycle.

I found a nice solution here:

http://groups.google.com/group/borl...0b0/dfc46e2bfaaa6cbc?lnk=st&#dfc46e2bfaaa6cbc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top