[C++-sig] Inheritance

Roman Yakovenko roman.yakovenko at gmail.com
Mon Jan 23 16:31:02 CET 2006


On 1/23/06, Xavier Baele <doremix at gmail.com> wrote:
> Hello,
>  I try to use inheritance in Boost.Python but I probably make something
> wrong because I have a strange error:
>
>  Here is the code:
>  "DeriveCpp" is a C++ class derived from a "Base" C++ class
>
>      flagCpp=pymg.DeriveCPP()
>      print flagCpp.getValue()     (*) --> OK
>      pymg.func(flagCpp)
>      print flagCpp.getValue()     (**)--> Error:
>                    Boost.Python.ArgumentError: Python argument types in
> DeriveCPP.getValue(DeriveCPP)
>                    did not match C++ signature: getValue(class Derive
> {lvalue})
>
>  while the first call (*) works, the second one (**) fails if I call the C++
> pymg.func(...) function between.
>
>  Any idea what is wrong in my code?  Here is the wrapping code for the
> classes and function
>
>                          Thanks a lot,
>
>                                    Xavier
>
>
>  class Base {
>  public:
>      virtual int getValue() = 0;
>  };
>
>  struct BaseWrap : Base, wrapper<Base> { // Warper is used because of the
> pure virtual function
>      int getValue() { return
> this->get_override("getValue")();}
>  };
>
>  class Derive : public Base {
>  public:
>      Derive() {}
>      virtual int getValue() { return 1000;}
>  };
>
>  struct DeriveWrap : Derive, wrapper<Derive> { // Warper is used because of
> the virtual function
>  public:
>      int getValue()  {
>          if (override getValue = this->get_override("getValue"))
>              return getValue();
>          return Derive::getValue();
>      }
>  };
>
>  void func_base(std::auto_ptr<Base> pkBase) // auto_ptr<> used here to give
> C++ ownership
>  {
>      // ...
>  }
>
>  BOOST_PYTHON_MODULE(pymg)
>  {
>      def("func", &func_base);
>
>      class_<BaseWrap, std::auto_ptr<BaseWrap>, boost::noncopyable>("Base")
>          .def("getValue", pure_virtual(&Base::getValue));
>
>      class_<DeriveWrap, std::auto_ptr<DeriveWrap>, boost::noncopyable,
> bases<Base> >("DeriveCPP")
>          .def("getValue", &Derive::getValue);
>
>      implicitly_convertible<
>           std::auto_ptr<DeriveWrap>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think that this should be Derive. I could be wrong. Test your use-case with
pyplusplus code generator. It has nice GUI and I am sure it can handle
your case.
Just try it. http://www.language-binding.net/pyplusplus/pyplusplus.html

>         , std::auto_ptr<Base>
>       >();
>  }
>

Roman Yakovenko



More information about the Cplusplus-sig mailing list