boost::python> exposing instances from c++

TheDustbustr thedustbustr at
Sun Jul 27 22:22:45 CEST 2003

I'm writing a game in C++ which calls out to python for scripting.  I'd like to
expose the instance of my Game class (a singleton) to python (so that the
instances of Clock, Camera, etc are available to the scripts).

I ran into trouble trying to expose a specific instance (or even a function
returning the instance).  Here is a simplification of my problem in code (c++):

class A{
    int i;
class B{
    A a;
B b;
B getb() {return b;} //return the instance

int main() {
    getb().a.i = 42;
    return 0;}
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
        .def("i", &A::i);
        .def("a", &B::a);
    def("getb", getb);

Of course, I would be calling any python script using these classes from within

This doesn't compile (using bjam with msvc).  If I return by reference or
pointers in getb it gets ugly (like pages and pages of error messages).  Am I
doing this the right way?  How do I expose an instance to python?

More information about the Python-list mailing list