News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ tips - Callbacks and multiple inheritance, part I

Started by ben2ong2, October 07, 2006, 04:00:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

This example is based upon "Subobject Members", Stephen Dewhurst,
C++ Report, V5 N3. This is part 1 of a multipart example.

Suppose that you need to model an engine and a button. When the
button is pressed, the engine is supposed to start. One convenient
way to handle this "callback" relationship is to use multiple
inheritance (we do this in Guide, SES/design, and Objectbench).

.................................................................

class ButtonCallback {
  public:
    virtual void callback () = 0;
};

.................................................................

class Button {
    ButtonCallback& target;
  public:
    Button (ButtonCallback& bcb) : target(bcb) {}

    virtual void press ();
};

void Button :: press ()
{
    target.callback ();
}

.................................................................

class Engine : public ButtonCallback {
    // ...
  public:
    void start ();
    virtual void callback ();
};


void Engine :: callback ()
{
    start ();
}

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login