¿Cómo agregar listas / señales de eventos a una clase de superman simple?

I can and would love to use boost or std for this. Sorry - I am new to C++. So I created a really simple program like:

#include <iostream>
#include <string>
using namespace std;

class superman
{
public:
  void punch(){cout << "superman: I hit the bad guy!" << endl;};
};

int main()
{
  superman clark;
  clark.punch();
  cin.get();
}

I want to add an event listner that would tell me when clark punched and cout something like "superman punched!". How to add such event listner and event function to my class?

preguntado el 08 de enero de 11 a las 16:01

cout << "superman: I hit the bad guy!" << endl; no devuelve un std::string . Which compiler are you using ? -

1 Respuestas

puedes usar boost::signal para que

you have to declare one class and overload her operator () after uoi have to bind the class on the signal, by using connect() method, and finally, use operator () of the signal

struct HelloWorld 
{
  void operator()() const 
  { 
    std::cout << "Hello, World!" << std::endl;
  } 
};

// ...

// Signal with no arguments and a void return value
boost::signal<void ()> sig;

// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);

// Call all of the slots
sig();

for using parameter in slot:

void print_sum(float x, float y)
{
  std::cout << "The sum is " << x+y << std::endl;
}

boost::signal<void (float, float)> sig;

sig.connect(&print_sum);

sig(5, 3);

Respondido el 08 de enero de 11 a las 22:01

I understand that it shall be done via signals bt I just do not see how to do it=( - Rella

you have look the tutorial? it's explain - kinnou02

i have add the example, with few comment - kinnou02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.