[C++-sig] design question

Neal Becker ndbecker2 at gmail.com
Thu Feb 22 15:16:20 CET 2007


I'm interested in any ideas/opinions on the following.  I am building c++
objects that are compositions of pieces.  The composition is done from
python.

For example, class D has a member of type A, where A's behavior is tuned
through polymorphism (traditional OO design).  Let's say B and C are
derived from A. Then, I can control this from python by:

b = B()
c = C()
d1 = D (b)
d2 = D (c)

So this can be exposed as:

-------------------------------
#include <boost/shared_ptr.hpp>

struct A {
  virtual void doit () = 0;
};

struct B : public A {
  virtual void doit ();
};


struct C {
  C (boost::shared_ptr<A> _a) :
    a (_a) {}

  void doit() { a->doit(); }

  boost::shared_ptr<A> a;
};

BOOST_PYTHON_MODULE (test)
{
  class_<A>, boost::noncopyable> ("A", no_init);
  class_<B, bases<A> > ("B"...);
  class_<C> ("C", init<boost::shared_ptr<A> >);
}
------------------------------

What I'm wondering is, is this a reasonable design?  Could it be made
significantly more efficient?




More information about the Cplusplus-sig mailing list