returning base class pointer from function

jfdeford at jfdeford at
Thu Dec 7 17:46:37 EST 2000

Okay, this is likely an easy one for somebody who
understands even a little bit about Python (this
apparently leaves me out).  I have built an
extension dll in C++, and via SWIG have the
appropriate shadow classes and such for the
various objects.  The situation is as follows: a
member function of one of my SWIGed C++ classes
returns a base class pointer (say class A) to an
object that is derived from A (class B : public
A).  Specifically, I have the following member
function declared in one of my classes:

	virtual A*	GetObject(const char
*pcPath, bool bCopyData);

wherein an object of type B is created and
returned (this function can return any one of a
number of objects that are all derived from A).
I can convince SWIG to create the appropriate
object (B) via Py_BuildValue prior to returning
it from the GetObject function in question (using
a typemap), and the shadow class just passes out
the object it gets from the wrapped GetObject
call, i.e. in the body of the shadow class
definition I find,

    def GetObject(self,arg0,arg1):
        val = pyordatac.PyORDatabase_GetObject
        return val

When I make an assignment at the command line


foo is nominally of "type" B, but it seems that I
can't use it to access functionality in B, e.g.,
foo.<func only in B>() gives me an attribute
error.  In C++ I would just cast the pointer to
the appropriate derived type - is this my only
option here?  If I do the following:


where swigged_B_classPtr is the shadow class
associated with my B class, then I can get at the
B class member functions.  I'd rather avoid
this "cast" step if possible, and I don't really
know what it's doing anyway.

Sent via
Before you buy.

More information about the Python-list mailing list