From: David P. <dpi...@me...> - 2009-10-06 22:30:52
|
This is normal behavior for SWIG C#. The problem is that SWIG has to convert the pointer returned by the getLastInserted function into a C# object, and it has no idea that a C# object already exists, so it creates a wrapper of type Obj to represent the pointer. Note that this problem exists whenever a C++ function returns a derived-class object in a base-class pointer, regardless of whether directors are involved. On the plus side, you can still call the virtual function fn() through the new wrapper. But I don't know if anyone has developed a workaround for this problem. Sorry. From: Giorgio Scorzelli [mailto:scr...@co...] Sent: Tuesday, October 06, 2009 11:03 AM To: swi...@li... Subject: [Swig-user] Problem of directors in C++ -> Csharp This is a very simple example of the problem I'm having with Swig directors /////////////////////////// in C++ //////////////////////// class Obj { public: Obj() {...} virtual void fn() {...} }; class Container { [...] public: Container(); void insert(Obj* obj); Obj* getLastInserted(); }; /////////////////////////// in Swig /////////////////////////// %feature("director") Obj; /////////////////////////// in CSharp /////////////////////////// class CsObj: Obj { CsObj() {...} public override void fn() {...} }; static void Main() { Container c=new Container(); CsObj o1=new CsObj(); o1.fn(); //OKvirtual is working c.insert(o1); CsObj o2=(CsObj)c.getLastInserted(); //ERROR HERE!!!! } I'm sure the object returned by getLastInserted is a CsObj, but .NET produce an error (impossible to cast). The strange thing is that the same example is perfecly working in Swig/Python! Do you have any idea how to solve the problem? Basically I need to get back the C# class. Thanks. |