osteam question

C

crichmon

Hi,

In a program I am working on I have messages sent to the screen for various
reasons. Rather then using cout directly, I'd like to send an ostream as a
parameter to a method of an object created to centralize messages. For
instance (I'm not saying this code works):

instead of

cout << "some helpful text " << x << " " << y << endl;

I write something to the effect of

MessageCenter.message( "some helpful text " << x << " " << y << endl );


Any insight on how to implement this properly?


thanks,
crichmon
 
T

Tobias Erbsland

Hi!
In a program I am working on I have messages sent to the screen for various
reasons. Rather then using cout directly, I'd like to send an ostream as a
parameter to a method of an object created to centralize messages. For
instance (I'm not saying this code works):
instead of
cout << "some helpful text " << x << " " << y << endl;
I write something to the effect of
MessageCenter.message( "some helpful text " << x << " " << y << endl );
Any insight on how to implement this properly?

I would use string streams:

#include <sstream>

void foo()
{
std::eek:stringstream msg;
msg << "some helpful text " << x << " " << y << std::endl;
MessageCenter.message( msg );
}

best regards
Tobias
 
T

Tobias Erbsland

Sorry a typo:

void foo()
{
std::eek:stringstream msg;
msg << "some helpful text " << x << " " << y << std::endl;
MessageCenter.message( msg.str() ); // msg -> msg.str()
}

best regards
Tobias
 
C

crichmon

Tobias Erbsland said:
Sorry a typo:

void foo()
{
std::eek:stringstream msg;
msg << "some helpful text " << x << " " << y << std::endl;
MessageCenter.message( msg.str() ); // msg -> msg.str()
}

So then would the message method take a string as input?

crichmon
 
T

Tobias Erbsland

So then would the message method take a string as input?

Yes. The message method declaration for example:

class MessageCenter
{
void message( const std::string& message );
}

best regards
Tobias
 
C

crichmon

Tobias Erbsland said:
Yes. The message method declaration for example:

class MessageCenter
{
void message( const std::string& message );
}

best regards
Tobias


Thanks a lot, that really helped me out. :)
crichmon
 

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
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top