[C++-sig] Re: Doubts with wrapping/using abstract bases.

Nicodemus nicodemus at globalite.com.br
Sun Aug 10 23:42:48 CEST 2003


Raoul Gough wrote:

>Prabhu Ramachandran <prabhu at aero.iitm.ernet.in> writes:
>  
>
>>Traceback (most recent call last):
>>  File "<stdin>", line 1, in ?
>>AttributeError: 'A' object has no attribute 'f'
>>
>>I did not expect this since this is perfectly legal in C++,
>>i.e. b1->f() in C++ is legal and used commonly.  So what am I doing
>>wrong?
>>    
>>
>
>How about adding the extra "def" manually to see whether it fixes the
>problem? Sorry I can't help on the Pyste side of this. Mayeb some
>semi-relevant stuff by Googling for pyste pure virtual.
>

Adding the f's def manually works, but problem is, you can now crash the 
interpreter fairly easily:

[d:\temp]python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from holder import *
 >>> b = B()
 >>> h = Holder()
 >>> h.add(b)
 >>> x = h.get(0)
 >>> x.f()
B::f
 >>> a = A()
 >>> a.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
 >>> a.f()

[d:\temp]

I think the solution is to provide a default implementation for f in 
A_Wrapper that raises an exception when called:

 >>> from holder import *
 >>> a = A()
 >>> a.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: abstract function called
 >>>

I implemented this, and the code is in CVS now. Thanks a lot once again 
Prabhu! 8)

Regards,
Nicodemus.





More information about the Cplusplus-sig mailing list