logo       

Re: C++ class inheritance using shadow class: msg#00081

programming.swig

Subject: Re: C++ class inheritance using shadow class


On Thu, 9 Sep 2004, Alexander Tournier wrote:

> I then swig it using shadow classes, that works fine. Now I would like
> to add extra python features to this classes, so I want to use inheritance.
>
> >>> class Sys(System):
> ... def __init__(self):
> ... print Sys.VoxelSize
> ...
> >>> c = Sys()
> <property object at 0x818ce24>
> >>> c.VoxelSize
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: Type error. Expected _p_System
> >>>
>
> The inheritance doesn't seem to work! Any reason why that should be??
> Looking forward to your suggestions,

In Python, __init__ does not get called up the inheritance hierarchy like
a C++ constructor. Your Sys.__init__ does no initialization before trying
to call a member function; no C++ object is instantiated, and the "this"
member is not set (all of that is done by System.__init__). If you did
not override __init__ in your subclass, the base class __init__ would be
called and all would be well. If you need to override __init__, putting a
call to System.__init__ at the beginning of Sys.__init__ might get you
what you want.

Josh

--
Joshua L. Cherry, Ph.D.
NCBI/NLM/NIH (Contractor)
jcherry@xxxxxxxxxxxxxxxx


_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise