> -----Original Message----- > From: Sisyphus [mailto:[EMAIL PROTECTED] > Sent: Sunday, June 19, 2005 11:06 PM > To: inline@perl.org > Subject: Re: Object Oriented Inline C > > > In other words,DESTROY() receives as its argument a SV*: > > DESTROY(SV * obj) {// do stuff} > > How does DESTROY() then determine whether it needs to do: > Soldier* soldier = (Soldier*)SvIV(SvRV(obj)); > or: > Civilian* civilian = (Civilian*)SvIV(SvRV(obj));
You could use the same trick that the perl sources themselves use when declaring RV, SV, HV, AV, and GV structures: make them share their first few fields. typedef struct { int is_soldier; } Person; typedef struct { int is_soldier; char* name; char* rank; long serial; } Soldier; typedef struct { int is_soldier; char* name; char* address; char* occupation; long age; } Civilian; Then in your code the first thing you can do is: Person* p = (Person*)SvIV(SvRV(obj)); if (p->is_soldier) { Soldier* soldier = (Soldier*) p; ... } else { Civilian* civilian = (Civilian*) p; ... } I've never actually tried this myself, because it seems like such a bad design, but it seems like it should work. -Ken