C # programmer trying for events in C ++

Hello everyone: I am an experienced C # programmer trying to do some work in C ++, and I'm not sure of the correct way to do this:

I am creating a class that should notify the consumer class that something has happened.

If I wrote this in C #, I would define an event in my class.

There are no events in C ++, so I'm trying to figure out how to do it right. I thought of callback functions, but how do I handle the case when I want to execute a member function (rather than a static function).

More specifically, I really need to do this in order to handle the event, but have access to the state of the participant in the instance of the object that processes the event.

I look at std :: tr1: function, but I am having problems with its operation.

I don't think anyone would like to translate the following C # example example into a good / good C ++ example (I need ANSI C ++)? (remember that I have almost no C ++ experience - do not assume that I know any long-established C ++ conventions - I do not;);

Simple C # console application (works on my machine):

using System; namespace ConsoleApplication1 { public class EventSource { public event EventHandler<EchoEventArgs> EchoEvent; public void RaiseEvent(int echoId) { var echoEvent = this.EchoEvent; if (echoEvent != null) echoEvent(this, new EchoEventArgs() {EchoId = echoId}); } } public class EchoEventArgs : EventArgs { public int EchoId { get; set; } } public class EventConsumer { public int Id { get; set; } public EventConsumer(EventSource source) { source.EchoEvent += OnEcho; } private void OnEcho(object sender, EchoEventArgs args) { // handle the echo, and use this.Id to prove that the correct instance data is present. Console.WriteLine("Echo! My Id: {0} Echo Id: {1}", this.Id, args.EchoId); } } internal class Program { private static void Main(string[] args) { var source = new EventSource(); var consumer1 = new EventConsumer(source) { Id = 1 }; var consumer2 = new EventConsumer(source) { Id = 2 }; source.RaiseEvent(1); Console.ReadLine(); } } } 
+4
source share
4 answers

The basic idea is to accept function objects, like std::function<Signature> as callbacks, for example. They are not function pointers, but can be called. The C ++ Standard Library (for C ++ 2011) contains a number of classes and functions, for example, std::mem_fn() and std::bind() , which allow you to use functions, including member functions, for use as function objects .

The part that is missing is something that supports multiple events: std::function<Signature> represents a single function. However, they are easy to put, for example, in std::vector<std::function<Signature>> . What becomes more interesting (and requires variational patterns to be executed easily) creates an event class that encapsulates the abstraction of several events, starts to register, is potentially unregistered, and is called.

+3
source

C ++ has the concept of a functor: the called object. You should read about them.

Think of an object that overwrites operator() . You are passing an instance of such an object. After that, you can call it a regular function. And he can maintain a state.

+1
source

There's also a Signals2 library in Boost that provides an API very close to the actual C # events, at least in the idiomatic sense.

+1
source

Qt has something that can help you call signals and slots: http://qt-project.org/doc/qt-4.8/signalsandslots.html

It allows you to specify which signals (events you want to listen to) and the slots (receiving side) of the object, and then you can connect them. More than one object can listen to the signal as you say what you need.

Qt is a large application platform, so I'm not sure how to use only signals and slots. But if you are creating an entire graphical application, then the rest of Qt can benefit you (many ui-style events are based on signals and slots).

+1
source

Source: https://habr.com/ru/post/1438098/


All Articles