[C++-sig] wrapping reference to c++ object

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Mar 13 18:43:28 CET 2003


--- harold fellermann <harold at imb-jena.de> wrote:
> class CPPObject : public PyObject
> {
> 	// [...]
> };
> 
> and as soon as I do this, my code is compiled without error, but failed 
> in
> the constructor of CPPObject. As stated in the Python/C-API, "you never 
> declare
> an automatic or static variable of type PyObject, only pointer 
> variables of
> type PyObject* can be declared." So how can the references be stored?

PyObjects are C objects. They have no constructor and destructor etc. Instead
you have to use a combination of Python C API functions to emulate what is
normally done by C++ constructors and destructors.

If you use Boost.Python's class_<> template all this will be done for you
automatically. This is pretty much the essence of Boost.Python. Please study
the fine tutorial to learn more about class_<>.

You can create wrapped instances of your C++ objects in various ways. The
simplest method is to copy an existing object into the newly created wrapped
object. But you can also copy references/pointers to existing C++ objects. This
is more involved because you have to think about and correctly deal with
lifetime issues. This is nicely explained in the tutorial under "Call
Policies."

Ralf


__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com




More information about the Cplusplus-sig mailing list