Re: [C++-sig] Wrapping a base class' method
Write a thin wrapper and .add_property that, e.g. (untested) int get_base_i_directly(my_class const& self) { return self.get_base_i(); } ... .add_property("basei", make_function(get_base_i_directly)) This way Boost.Python only "sees" my_class in the signature. ----- Original Message ---- From: "bernhard.maeder@zkb.ch" <bernhard.maeder@zkb.ch> To: c++-sig@python.org Sent: Sunday, November 19, 2006 11:30:28 PM Subject: [C++-sig] Wrapping a base class' method Hello all I'm trying to export some base class' methods with the derived class directly. See the following code: // ----------------------------------------------------- #include <boost/python.hpp> struct my_base_class { int const & get_base_i() const { return i_; } int i_; }; struct my_class : my_base_class { int const & get_i() const { return i_; } int i_; }; using namespace boost::python; BOOST_PYTHON_MODULE(_test) { class_<my_class>("MyClass") .add_property("i", make_function(&my_class::get_i, return_value_policy<copy_const_reference>())) .add_property("basei", make_function(&my_class::get_base_i, return_value_policy<copy_const_reference>())) ; } // ----------------------------------------------------- Now python throws the following at me: h[ 1] >>> from _test import * h[ 1] >>> MyClass().basei Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in None.None(MyClass) did not match C++ signature: None(my_base_class {lvalue}) So, my question is: Am I not supposed to do this at all? The same seems to work flawlessly when returning by value instead of by reference.... Thanks for your input Bernhard ___________________________________________________________________ Disclaimer: Diese Mitteilung ist nur fuer die Empfaengerin / den Empfaenger bestimmt. Fuer den Fall, dass sie von nichtberechtigten Personen empfangen wird, bitten wir diese hoeflich, die Mitteilung an die ZKB zurueckzusenden und anschliessend die Mitteilung mit allen Anhaengen sowie allfaellige Kopien zu vernichten bzw. zu loeschen. Der Gebrauch der Information ist verboten. This message is intended only for the named recipient and may contain confidential or privileged information. If you have received it in error, please advise the sender by return e-mail and delete this message and any attachments. Any unauthorised use or dissemination of this information is strictly prohibited. _______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Do you Yahoo!? Everyone is raving about the all-new Yahoo! Mail beta. http://new.mail.yahoo.com
Write a thin wrapper and .add_property that, e.g. (untested)
int get_base_i_directly(my_class const& self) { return self.get_base_i(); } ... .add_property("basei", make_function(get_base_i_directly))
This way Boost.Python only "sees" my_class in the signature.
Yes, thanks, that works. What puzzles me, is that the same works for return-by-value wrappings. I don't quite get why that's something different to boost.python. Is there an easy explanation or am I just missing the obvious? Thanks Bernhard ___________________________________________________________________ Disclaimer: Diese Mitteilung ist nur fuer die Empfaengerin / den Empfaenger bestimmt. Fuer den Fall, dass sie von nichtberechtigten Personen empfangen wird, bitten wir diese hoeflich, die Mitteilung an die ZKB zurueckzusenden und anschliessend die Mitteilung mit allen Anhaengen sowie allfaellige Kopien zu vernichten bzw. zu loeschen. Der Gebrauch der Information ist verboten. This message is intended only for the named recipient and may contain confidential or privileged information. If you have received it in error, please advise the sender by return e-mail and delete this message and any attachments. Any unauthorised use or dissemination of this information is strictly prohibited.
participants (2)
-
bernhard.maeder@zkb.ch -
Ralf W. Grosse-Kunstleve