[C++-sig] Static, again...

Hugo van der Merwe hugo at adept.co.za
Tue Nov 12 11:07:24 CET 2002

I had a look at the mailing list archives about this topic, I'm still
not clear on how exactly it works:

> #include <boost/python/module.hpp>
> #include <boost/python/class.hpp>
> struct Num {
>    Num() {}
>    ~Num() {}
>    static int getDims() {return 3;}
>    int dims() {return 5;}
> };
> {
>    using namespace boost::python;
>    module numTestMod("numTest");
>    numTestMod
>       .add
>       ( class_<Num>("Num")
>         .def_init()
>         .def("getDims",&Num::getDims)
>         .def("dims", &Num::dims)
>         )
>       ;
> }

> We don't have a way to make true static methods yet.
> getDims will work fine if you access it through the class like this:
>     Nums.getDims()

I am trying to wrap a "Settings" class which does not have a public
constructor, it expects you to call "GetInstance" to get a pointer to
the always-existing instance (making the settings "global"). The above
would make me think that this will work:

	class_<Settings, boost::noncopyable>("Settings", no_init)
	.def("GetInstance", &Settings::GetInstance,

I cannot call Settings.GetInstance(), it gives me:

TypeError: unbound method Boost.Python.function object must be called with Settings instance as first argument (got nothing instead)

I also read about an approach that looks something like this:

    object settings_class =
	class_<Settings, boost::noncopyable>("Settings", no_init);
    settings_class.attr("Settings") = &Settings::GetInstance;

But I don't know how that is supposed to work...

If I wanted to expose GetInstance as Settings() instead (once I get it
working), is it as simple as just calling it "__init__" instead?
(Should I then have no_init in the class_<> invocation or not? I
noticed the tutorial talks about no_init, but then mentions as soon as
you wish to derive classes from it, you do not want no_init. So when
is no_init useful? Only when you cannot construct a class and don't
want to derive from it, i.e. when you're using "factories"?)

I am really astonished with what can be achieved with C++ -
Boost.Python is really amazing!

Thanks a lot,
Hugo van der Merwe

More information about the Cplusplus-sig mailing list