Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Hitman

callbacks in C++

This topic is 6943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am fairly new to C++, and was wondering what people thought was the best way to implement callbacks. One way would be to implement the observer pattern, but then I would probably have to use multiple inheritance in some cases (ie objects that have a base class but also want to be called back). Are there any problems with using multiple inheritance. Thanks for any help, Bret

Share this post


Link to post
Share on other sites
Advertisement
You don''t need multiple inheritance to use the observer pattern. You can define one class (perhaps abstract) for the "subject" (which will call the observer) and one interface class (surely abstract) for the observer. To use an object you already got just inherit the observer and create an observer which will now you object and calls it. If you are into patterns this is something like an "adapter".

Another way to implement callbacks could be to use the so called "command" pattern (One interface class for a command is inherited to get a class which nows your to be called objects. You command is registered by the "subject". If the subject calls the command (command->execute() it calls the methods on your object). However all these pattern are looking really similar when used in the way you described it.

If you would describe you problem in more detail you will perhaps get better and more specific answers.

Bjoern

Share this post


Link to post
Share on other sites
quote:
Original post by bjoern

You don''t need multiple inheritance to use the observer pattern. You can define one class (perhaps abstract) for the "subject" (which will call the observer) and one interface class (surely abstract) for the observer. To use an object you already got just inherit the observer and create an observer which will now you object and calls it. If you are into patterns this is something like an "adapter".

Another way to implement callbacks could be to use the so called "command" pattern (One interface class for a command is inherited to get a class which nows your to be called objects. You command is registered by the "subject". If the subject calls the command (command->execute() it calls the methods on your object). However all these pattern are looking really similar when used in the way you described it.

If you would describe you problem in more detail you will perhaps get better and more specific answers.

Bjoern


So the observer class inherits from the abstract class? What if that class also inherits from another (un-related) class will this not result in multiple inheritance?

What I''m trying to do is create a wrapper class for direct input. Objects will register with this wrapper class to be informed when certain events occur (ie keypresses, mouse click, mouse move, mouse drag, joystick etc.). Objects will have focus and the object with focus (ie a window, not a windows window, but one of my own creation in order to be displayed on a direct draw surface) for the particular event will then be informed and can react. I figure this is best done with some sort of callback to the object when an event happens. So you can see that an object (window) will already have a base class but will also have to inherit from the observer abstract class. I have been told to stay away from multiple inheritance if possible (don''t know the reason why, hence the question).

Thanks,
Bret

Share this post


Link to post
Share on other sites
Multiple inheritance isn''t bad by itself - however it can make things complicated and you have to remember the memory- and performance-costs of multiple inheritance like you will have with other forms of inheritance too.

One guideline to follow concerning multiple inheritance is just to inherit from abstract or interface classes. Take a look into Scott Meyers "More Effective C++".

Nevertheless for your case you don''t need multiple inheritance. Take a look:

class CWillCallCallbacks

class IObserver;

class CYourObject;

class CYourObserver: public IObserver

(Hopefully this board doesn''t kick funny symbols into the text...)

CYourObserver contains a pointer to CYourObject and will call it whenever CWillCallCallbacks calls CYourObeserver. You see - CYourObserver is just a wrapper which will tell CYourObject all the messages it gets.

You can use multiple inheritance indeed - however, only use it if it makes sence - not if it could make your job easier. However if you know your performance needs multiple inheritance could be faster - but I don''t think that it is worth the hassle.

I don''t understand you example - it looks like there will only be one object registered to get callbacks. The other way round it is totally unclear for me how a window knows if a mouse click is for itself...

Bjoern

Share this post


Link to post
Share on other sites
>> a window knows if a mouse click is for itself...
I implement hotspot observers for this.
For eg, some object, like a user control sprite, in my case, are derived from a few abstract interfaces.
IHotSpot, IKeyDownEvent, IActor, IPersistentObject
IHotSpot defines a hotspot that can be clicked upon or have effects upon mouse over. I add this ''callback''/observer to my Input System, upon my IKeyDownEvent. Upon polling my input system, they run through the list, and check the object for events happening that should be handled.
Similiar for IActor, which when added to my stage, can be drawn onto the screen if visible, check collision against other actors(through this IActor''s member methods).
I will be going on to implement a world system as ZoomBoy has talked about in the other thread in AI, where each actor furthur implements another interface(undecided), and listens to events happening in the world and act accordingly.

Summary, Event driven system rocks. Made my code very nice and organized. Very OO

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!