Virtual functions vs callbacks

Consider a scenario in which there are two classes, i.e. basic and derivative. If the base class wants to call the function of the derived class, it can do this by creating a virtual function and defining this VF in the derived class or using callbacks. I want to know which should be preferred from the two? The choice between them depends on what situations / conditions?

EDIT: Clarification of the question:

The situation that I talked about is that there is a base class that receives messages. These different messages must be handled differently by the derived class, so one way is to create a virtual function and let the derived class implement it by processing each message using different switching cases.

Another way is to implement callbacks through function pointers (pointing to functions of a derived class) inside templates (templates are needed to process an object of a derived class and function names). Templates and function pointers will be in the base class.

+4
source share
3 answers

A virtual function call is a callback.

The caller searches for the corresponding entry in the virtual function table of the object and calls it. This is exactly the same as a callback, except that member function pointers have awkward syntax. Virtual functions offload the compiler, making them a very elegant solution.

Virtual functions are a way of communicating in an inheritance hierarchy.

+4
source

I think that it comes down to deciding whether the behavior you are talking about is something that belongs to the hierarchy that the Base knows and implements the child.

If you come with a callback solution, then the callback method (depending on the signature) should not be implemented in the Base child. This may be appropriate if, for example, you wanted to say “this event happened” with a “listener of events”, which may be in a derived class or may be in a completely unrelated class, which seems to be interested in the event.

If you go with solving a virtual function, then you are more closely related to implementing the Derived and Base classes.

An interesting read that might somehow answer your question: C ++ callbacks that talk about using functors. There is also a Wikipedia example that uses a template callback to sort. You will notice that the implementation for the callback (which is a comparison function) does not have to be in the sorting object. If this were implemented using virtual methods, this is not so.

+4
source

I do not think that the two cases you are describing are comparable. Virtual functions are a polymorphism tool that helps you extend the base class to provide additional functionality. Their key characteristic is that a decision whose function will be called is executed at run time.

Callbacks are a more general concept that does not apply only to parent class relationships.

So, if you want to do this, enable the base class extension, I would certainly go with virtual functions. However, keep in mind how virtual functions work.

+1
source

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


All Articles