Virtual functions versus Callbacks
Asked Answered
M

3

7

Consider a scenario where there are two classes i.e. Base and Derived. If the Base class wants to call a function of the derived class, it can do so by either making a virtual function and defining that VF in the derived class or by using callbacks. I want to know in what should be preferred out of the two? Choosing among the two depends on which situations/conditions?

EDIT: Question Clarification:

The situation I was referring to is that there is a base class which receives messages. These different messages are to be handled differently by the derived class, so one way is to create a virtual function and let the derived class implement it, handling every message though various switch cases.

Another way is to implement the callbacks through the function pointers (pointing to the functions of derived class) inside the templates (templates are needed for handling the object of the derived class and the function names). The templates and the function pointers are going to reside in the base class.

Mauramauralia answered 6/4, 2011 at 10:12 Comment(3)
I think you would need to provide a concrete example to clarify your question a bit if you are stilling looking for an answer. I don't understand why you would need 'various switch cases' for example with option one. There are usually ways round this through program design. In your second option, it's unclear what you mean by templates. Are you talking about C++ templates, or some kind of message template that contains information to identify the function pointers? Something more concrete, even if it were pseudo code would be helpful.Theadora
@Theadora Thanks for responding, I'll follow up in a few days.Mauramauralia
@Theadora Pardon me for not following up on this topic. I will get back to this in a few days, there are some other questions pending too. I remember them completely, but currently I have too many dishes up on my plate. will follow up soon.Mauramauralia
T
5

I think this comes down to a decision about whether or not the behaviour you're talking about is something that belongs in the heirarchy that 'Base' knows about and child implements.

If you go with a callback solution, then the callback method (depending on signature) doesn't have to be implemented in a child of Base. This may be appropriate if for example you wanted to say 'this event has happened' to an 'event listener' that could be in a derived class, or could be in a totally unrelated class that happens to be interested in the event.

If you go with the virtual function solution, then you're more tightly coupling the implentation of the Derived and Base classes.

An interesting read, which may go some way to answering your question is: Callbacks in C++ which talks about the usage of Functors. There's also an example on Wikipedia that uses a template callback for sorting. You'll notice that the implementation for the callback (which is a comparison function) does not have to be in the object that is performing the sort. If it were implemented using virtual methods, this wouldn't be the case.

Theadora answered 6/4, 2011 at 10:21 Comment(1)
In my case the base class doesn't know the derived class function which is going to handle the change w.r.t the derived class. I'll take a look at your link, thanks.Mauramauralia
W
6

A virtual function call is actually a callback.

The caller looks up the corresponding entry in the object's virtual function table and calls it. That's exactly like a callback behaves, except that member function pointers have awkward syntax. Virtual functions offload the work to the compiler, which makes them a very elegant solution.

Virtual functions are the way to communicate within the inheritance hierarchy.

Wolfe answered 6/4, 2011 at 10:21 Comment(3)
Thanks for responding, but I was referring to the C++ callbacks which are implemented through templates. Secondly there must be a reason that either of the two is preferred on a given situation?Mauramauralia
The term callback is a bit vague. Please give an example that we can fine-tune our responses (i.e. add it to your question)Wolfe
@kgiannakakis , @Theadora : I have edited the question for more clarification, if it is still unclear, please let me know.Mauramauralia
T
5

I think this comes down to a decision about whether or not the behaviour you're talking about is something that belongs in the heirarchy that 'Base' knows about and child implements.

If you go with a callback solution, then the callback method (depending on signature) doesn't have to be implemented in a child of Base. This may be appropriate if for example you wanted to say 'this event has happened' to an 'event listener' that could be in a derived class, or could be in a totally unrelated class that happens to be interested in the event.

If you go with the virtual function solution, then you're more tightly coupling the implentation of the Derived and Base classes.

An interesting read, which may go some way to answering your question is: Callbacks in C++ which talks about the usage of Functors. There's also an example on Wikipedia that uses a template callback for sorting. You'll notice that the implementation for the callback (which is a comparison function) does not have to be in the object that is performing the sort. If it were implemented using virtual methods, this wouldn't be the case.

Theadora answered 6/4, 2011 at 10:21 Comment(1)
In my case the base class doesn't know the derived class function which is going to handle the change w.r.t the derived class. I'll take a look at your link, thanks.Mauramauralia
E
2

I don't think that the two cases you are describing are comparable. Virtual functions are a polymorphism tool that aid you in extending a base class in order to provide additional functionality. The key characteristic of them is that the decision which function will be called is made in runtime.

Callbacks are a more general concept, that doesn't apply only on a Parent-Child class relationship.

So, if you want to do involves extending a base class, I would certainly go with virtual functions. Be sure however to understand how virtual functions work.

Ensemble answered 6/4, 2011 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.