Hi Brian,


On Wed, Nov 7, 2012 at 4:04 PM, Brian Budge <brian.budge@gmail.com> wrote:
Hi all -

I have a case where I have a largeish inheritance hierarchy.  I will be calling an initialization function on all objects before using them in some fashion.

Basically what I need to happen is when I call init() on a Foo the following would occur:

void Foo::init() {
    super::init();
    // now with the knowledge that anything I inherited is set up, I can set myself up...
}

When I'm dealing with a situation like you describe (not using constructors), I find that Boost.Signals (or Signals2) do what I'm looking for.

I generally do something like the following:

class B
{
  typedef boost::signal<void()> OnInitListener;
  OnInitListener onInit_;
public:
  boost::signals::connection connectOnInitListener(OnInitListener::slot_function_type cb)
  { return onInit_.connect(cb); }

  void onInit() { onInit_(); }
};

class D : public B
{
  D() { connectOnInitListener(bind(&D::onInitImpl, this)); } // Register our onInit handler.
};

//Repeat for further derivatives.

This means I don't need to state whom I inherit from, the order of calling is determined by the order of construction, and if one method doesn't need to listen for the onInit signal, everything works as expected.

HTH,
Nate