inheritance

J

J

Hi


I am trying to make a C++ class hierarchy accessible to python scripts.
Am I right to assume that the type structure of a derived class must
have all the members of its base class declared as well ?

For example,

typedef struct
{
PyObject_HEAD
int somebaseclassmember;
} PyStructBaseClass

is the structure for a base class. In this case, the structure for a
derived class would have to look like

typedef struct
{
PyObject_HEAD
ScnNode* mObject;
int somebaseclassmember;
float somederivedclassmember

} PyStructDerivedClass;


Is this true ? I have linked the derived class type to the base class
type via tp_base. I don't think that when I instantiate a derived
class, a PyStructBaseClass is being created. I believe that if
PyStructDerivedClass was missing the "int somebaseclassmember;" then
the offset for that memory would point into the float during base class
member lookups .... ???

Any thoughts ?

Cheers
Jochen
 
J

J

just to follow up. I managed to get it working by having a closer look
at the xxmodules. However, the example does the following

typedef struct
{
PyStructBaseClass mBase;
int mValue;
} PyStructDerivedClass;

Does anything speak agains doing this ?

typedef struct : public PyStructBaseClass
{
int mValue;
} PyStructDerivedClass;

It seems to work for me. The advantage is that access to the members in
that structure is easier. The .mBase is not needed ....

Cheers
Jochen
 
P

Paul McGuire

I'm not intimate with Python's C/C++ implementation, but is it possible
that your modified code may not be implementation portable? For
instance, if some calculation of the size of the object is required,
inherited classes may layout data differently than contained classes
would, especially if classes do not contain a "nice" (i.e.,
word-boundary) number of bytes.

Or is it possible that there are other Python routines that access the
mBase field, perhaps for some form of introspection? What you might do
is convert one of the existing typedefs to use your form, then build an
run Python regression tests, preferably on Win, Mac, and *nix. Or at
least run your own alternative PyStructDerivedClass through a series of
tests that specifically target and exercise the class hierarchy.

(I'm sorry if I'm proposing an onerous build/test burden, but if you
want your C++ code to generally adopted, while straying from accepted
practices, then you should probably prepare to put in some extra effort
to validate your alternative approach.)

I agree, your approach looks much cleaner. Unfortunately, in the face
of having to support many platforms and compilers, Python
implementation and extension may constrain developers to a clearly
portable subset of language features.

-- Paul
 
J

James Dennett

J said:
just to follow up. I managed to get it working by having a closer look
at the xxmodules. However, the example does the following

typedef struct
{
PyStructBaseClass mBase;
int mValue;
} PyStructDerivedClass;

Does anything speak agains doing this ?

typedef struct : public PyStructBaseClass
{
int mValue;
} PyStructDerivedClass;

It seems to work for me. The advantage is that access to the members in
that structure is easier. The .mBase is not needed ....

Well, it's C++, not C, and in C++ you wouldn't idiomatically
use typedef there, but rather write

struct pyStructDerivedClass : PyStructBaseClass
{
int mValue;
};

You'll also be relying on assumptions of how single inheritance
is implemented by your C++ compiler -- but that said, I've used
this assumption in the past and found it to be valid for a wide
range of compiler/ABIs.

-- James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top