Hi, I have read the paper of Boost.Python and the tutorial, and started trying wrapping some handy-made classes to learn how to use Boost.Python with real APIs. My question is simple (I think :) ). Suppose I have this class below: ### a.h ### class A { A* _parent; char* _name; public: A(A* parent=0, const char* name); const char* getName() const; void setName(const char* name); A* getParent() const; }; ### a.cpp ### #include "hello.h" #include <string.h> A::A(A* parent, const char* name) { _parent = parent; setName(name); } const char* A::getName() const { return _name; } void A::setName(const char* name) { strcpy(_name, name); } A* A::getParent() const { return _parent; } --- The example is simple enough, besides that some functions deals with char* and A* pointers. Reading the turorial and paper I've noticed that the arguments and return values are always ints, bools, doubles and even std::strings, but never custom objects references/pointers. I have tried (trivial) examples before (no pointers) and it worked Ok. But trying this example things do not worked as I expected. I tried exposing this class with something like: #include <hello.h> BOOST_PYTHON_MODULE(hello) { class_<A>("A", init<A*, const char*>()) .add_property("name", &A::getName, &A::setName) // readwrite property .add_property("parent", &A::getParent) // readonly property ; } Here is my Python script that uses the wrapper: ### hello.py ### import hello a = hello.A(None, 'a') print a, a.name, a.parent b = hello.A(a, 'b') print b, b.name, b.parent --- The expected behaviour is that the "a" object has no parent, so I pass a None (which I suppose that is converted to 0 in C++), and a "b" object has "a" as its parent. The expected output is:
import hello a = hello.A(None, 'a') print a, a.name, a.parent <hello.A object at 0x11111111> a None b = hello.A(a, 'b') print b, b.name, b.parent <hello.A object at 0x22222222> b <hello.A object at 0x11111111>
--- Thanks in advance. I think if I understand this example things will be much clearer from here on... [Eric Jardim]
Hehe, just a little mistake: The "a.h" and "a.cpp" files are actually "hello.h" and "hello.cpp". Sorry :) [Eric Jardim]
Eric Jardim <ericjardim@gmail.com> writes:
.add_property("parent", &A::getParent) // readonly property
Look into using call policies for this one. http://www.boost.org/libs/python/doc/tutorial/doc/html/python/functions.html... HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams -
Eric Jardim