[C++-sig] Inheriting static overloaded properties

Holger Brandsmeier brandsmeier at gmx.de
Tue Sep 4 10:13:07 CEST 2012


Dear Karl,

your problem does not occur if you do the following,
    bp::class_<Base>("Base")
        .add_static_property("debug", &Base::debug, &Base::setDebug);

    bp::class_<Derived, bp::bases<Base> >("Derived")
        .add_static_property("debug2", &Derived::debug, &Derived::setDebug);

Note that I called the property "debug2" in the drived class. I am not
sure if this is an option for you though.

What I think is the problem is easier to explain for another method,
`staticmethod`, which declares that a member function is static. In
the documentation it explicitly states:

"Note: Attempting to invoke def(name,...) after invoking
staticmethod(name) will raise a RuntimeError."
http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/class.html

Boost python can at the moment not support if you declare something a
static method under some name in python, and later you try to overload
this function with something else in python. Your error makes me
believe that the same thing is true for  add_static_property.

-Holger


On Tue, Sep 4, 2012 at 9:04 AM, Charly Bicker <legordian at gmx.net> wrote:
> Dear all,
>
> I am working in the following scenario: I have a class "Derived" which inherits from "Base" and wrappers for both. Both have their own static debug flag and getters and setters for it (see code at the end of the mail). I try to use ".add_static_property" to make the debug flags visible in python, however if I do this for both Base and Derived, I get:
>
> python -c "import testInheritance"
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
> Boost.Python.ArgumentError: Python argument types in
>     None.None(Boost.Python.StaticProperty)
> did not match C++ signature:
>     None(bool)
>
> Already when trying to import the module. If I do not add the property for Derived, I can import the module but then using debug on the Derived set the debug for Base instead. What am I doing wrong? Is this a bug? Does anybody know a solution? Any help would be greatly appreciated!
>
> Best regards,
> Karl Bicker
>
> PS: I would also like to make you aware of a stackoverflow question of mine: http://stackoverflow.com/questions/12202763/boost-python-inheriting-from-wrapped-classes
>
> Code:
> ----------------------------------------------------------------------------
> #include<boost/python.hpp>
>
> namespace bp = boost::python;
>
> struct Base {
>
>     Base() { };
>
>     static bool debug() { return _debug; };
>     static void setDebug(const bool debug = true) { _debug = debug; };
>
>   private:
>     static bool _debug;
>
> };
>
> struct BaseWrapper : Base,
>                      bp::wrapper<Base>
> {
>
>     BaseWrapper() :
>         Base(),
>         bp::wrapper<Base>() { };
>
>     BaseWrapper(const Base& base) :
>         Base(base),
>         bp::wrapper<Base>() { };
>
> };
>
> struct Derived : Base
> {
>
>     Derived() : Base() { };
>
>     static bool debug() { return _debug; };
>     static void setDebug(const bool debug = true) { _debug = debug; };
>
>   private:
>     static bool _debug;
>
>
> };
>
> struct DerivedWrapper : Derived,
>                         bp::wrapper<Derived>
> {
>
>     DerivedWrapper() :
>         Derived(),
>         bp::wrapper<Derived>() { };
>
>     DerivedWrapper(const Derived derived) :
>         Derived(derived),
>         bp::wrapper<Derived>() { };
>
> };
>
> bool Base::_debug = false;
> bool Derived::_debug = false;
>
> BOOST_PYTHON_MODULE(testInheritance){
>
>     bp::class_<BaseWrapper>("Base")
>         .add_static_property("debug", &BaseWrapper::debug, &BaseWrapper::setDebug);
>
>     bp::class_<DerivedWrapper, bp::bases<Base> >("Derived")
>         .add_static_property("debug", &DerivedWrapper::debug, &DerivedWrapper::setDebug);
>
> }
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list