Boost logo

Boost Users :

Subject: [Boost-users] pattern support for traversing inheritance chain
From: Brian Budge (brian.budge_at_[hidden])
Date: 2012-11-07 18:04:08


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...
}

It is not necessary for every call to implement init, in which case the
behavior should be that the call is propagated up the inheritance chain
(which it is by default in C++).

In order to make this work, I need to write different code for "super" in
every class implementation, which is a bit error prone. Even if I don't
copy/paste, my brain might be on autopilot and type something further up
the inheritance chain than it should be. Alternatively, I could require
that each class provides a typedef for super, but I'm still relying on
myself to remember to call super's init function.

Is there anything in boost that would support this kind of hierarchy
traversal (semi-)automatically? If there is not, are there suggestions for
ways to implement this in a less error-prone fashion?

Thanks,
  Brian



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net