| Store | Cart

[C++-sig] Embedding Python - creating Python objects from C++

From: Sohail Somani <soh...@taggedtype.net>
Sun, 29 Nov 2009 19:50:13 -0500
I have a class in C++ as follows:

class Base
{
   virtual ~Base() /* ideally like = 0 but doesn't seem to work */;
};

I export this class to Python:

class_<Base>("Base",no_init);

The user is expected to create a derived class:

class UserClass(Base):
   ... stuff here ...


What I want to do then (in C++) is create instances of this Python class 
that are stored using a shared pointer:

  // constructs a new instance of UserClass in Python using magic
  // but uses shared_ptr to keep alive
shared_ptr<Base> base(/* magic here */);

I understand that I can do something like module.attr("UserClass")() to 
create an instance but that will return an object instance which doesn't 
seem like it would play nice if I extracted a Base pointer from it.

So does anyone know how to implement the above referenced magic? It 
sounds like it should be obvious, but I'm not sure.

Thanks, and sorry if this is a newb question!

Sohail

Recent Messages in this Thread
Sohail Somani Nov 30, 2009 12:50 am
Sohail Somani Nov 30, 2009 07:09 pm
Messages in this thread