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

David Abrahams dave at boost-consulting.com
Thu Dec 5 03:21:45 CET 2002


"Scott A. Smith" <ssmith at magnet.fsu.edu> writes:

> I also have looked over the mailing list archives and 
> still don't see how things work. The example given was
>
>> #include <boost/python/module.hpp>
>> #include <boost/python/class.hpp>
>>
>> struct Num {
>>    Num() {}
>>    ~Num() {}
>>    static int getDims() {return 3;}
>>    int dims() {return 5;}
>> };
>>
>> BOOST_PYTHON_MODULE_INIT(numTest)
>> {
>>    using namespace boost::python;
>>    module numTestMod("numTest");
>>    numTestMod
>>       .add
>>       ( class_<Num>("Num")
>>         .def_init()
>>         .def("getDims",&Num::getDims)
>>         .def("dims", &Num::dims)
>>         )
>>       ;
>> }
>
> Was the conclusion in this discussion that one cannot
> use getDims with an instance of the class NOR with the
> class itself? That is neither
>
> X = Num()
> X.getDims()
>
> nor
>
> Num.getDims()
>
> will work? 

I don't know if we concluded that, but it's correct.

> Neither works for me. David wrote that
>
>> We don't have a way to make true static methods yet.
>
> So, what ways are there for using the static method? 

A static member function is just a regular function; you can add it at
module scope:

BOOST_PYTHON_MODULE_INIT(numTest)
{
   def("getDims", &Num::getDims);
   ...
}

> As I recall, using the static method in Python via the class name
> worked OK in BPV1. 

I think that's correct.

> I cannot get function getDims() working in V2 with anything I've
> tried. Can someone spell it out for me?

See above.

> Thanks, (and thanks to Dave twice over in a single day)

If it's any consolation, I now know exactly what needs to be done to
support static methods. It's just a matter of finding time and/or
funding to do the work...

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list