I have three objects in a class (channelManager) that i need to call in my main class in order to output a message but i can't seem to know how since i'm not a c++ expert. I think my porblem is that i don't really understand the arguments of these three objects:

void ChannelManager::setOutputStream(std::ostream* stream) {
    m_output_stream = stream;

void ChannelManager::outputMessage(string ip, string message){
    if (m_output_stream != NULL) {
        (*m_output_stream) << message;
    if (m_message_callback != NULL) {
        m_message_callback(ip, message);

void ChannelManager::setMessageCallback(function<void(string, string)> message_callback) {
    m_message_callback = message_callback;

mainly, i want to create three functions in order to be able to call these objects to output the message.
if anyone asks i can provide the code of my main class.

1 Month
Discussion Span
Last Post by ravenous

Hmm, what you have shown here are three functions or methods from a class. Two of the methods are used in the setup of your output (setOutputStream and setMessageCallback) and the other (outputMessage) is used to actually output the things that you want to output.

You haven't given much code to go on, but from what you have given, it looks like you'd use it like this:

// Actually create the ChannelManager object. I have no idea where
// this will be done in your case, or how. But I assume that you have
// an object somewhere.
auto cm = ChannelManager();

// Now we call the two setup functions
// Set the output stream to stdout

// Make a callback function to set into cm
std::ofstream log_file("log_file.txt", std::ios::app);
std::function<void(string, string)> log_file_writer = [&log_file](std::string ip, std::string msg){
    log_file << ip << ": " << msg << std::endl;

// Set that callback into cm

// We've registered the output stream and a callback, let's output
// a message...
// Actually call cm to output some message
cm.outputMessage("", "Hello, Localhost!");

There are a bunch of things in this code that you may, or may not, be familiar with. If you don't understand you should try looking up C++ std::function and lambdas in C++. That's what all that stuff in the middle is using when setting up the callback. Also, in this case, I've made something that logs things to a file, but you could make it do almost anything you want in the callback function; that's kind of the point of the callback pattern.

So, in this example, when you call the outputMessage function, it checks to see if there is a non-NULL output stream and, if so, it writes message to that stream. Once it's done that, outputMessage checks if there is a non-NULL callback. If so, it calls the callback function with the ip and message variables as arguments. In the case above, the callback function the writes those things to log_file.txt.

(Also, I didn't compile any of this, and I have a feeling that there are some rules about capturing variables in lambdas that are then cast to std::function objects that I might be breaking. This is code is just for example though, so treat it as psuedo-code ;) )

Edited by ravenous

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.