From: Fabien R. <fab...@cy...> - 2008-10-29 08:38:07
|
Fabien Rohrer wrote: > > > wsfulton wrote: >> >> Fabien Rohrer wrote: >>> Hi, >>> >>> >>> I'm trying to extend a C++ third part library to Java and I can't >>> override the code of a constructor. >>> >>> >>> Let say that I have a file MyClass.hpp: >>> >>> class MyClass{ >>> public: >>> MyClass(){ >>> cout << "Hello" << endl; >>> } >>> } >>> >>> Is it possible to override this constructor into the SWIG interface >>> file? >>> >>> I tried something like: >>> >>> %ignore MyClass::MyClass(); >>> %include <MyClass.hpp> >>> %extend MyClass { >>> MyClass(){ >>> cout << "Hello World" << endl; >>> } >>> }; >>> >>> But the ignore statement is still valid when the extend statement is >>> applied. So no constructor is generated. >>> >>> Can anybody help me? Any suggestion is welcome! >>> >> Move the %extend to above the %ignore and it will work. >> >> William >> >> ------------------------------------------------------------------------- >> This SF.Net email is sponsored by the Moblin Your Move Developer's >> challenge >> Build the coolest Linux based applications with Moblin SDK & win great >> prizes >> Grand prize is a trip for two to an Open Source event anywhere in the >> world >> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >> _______________________________________________ >> Swig-user mailing list >> Swi...@li... >> https://lists.sourceforge.net/lists/listinfo/swig-user >> >> > > Thanks for your consideration and your quick answer. But it appears to be > not so easy. The following code you suggest: > > %extend MyClass { > MyClass(){ > cout << "Hello World" << endl; > } > }; > %ignore MyClass::MyClass(); > %include <MyClass.hpp> > > raise the following warning: > > Warning(303): %extend defined for an undeclared class MyClass. > > Is there a way to declare this class before the extend statement? > > Fabien Rohrer > I apologize; I wrote an answer too quickly. My error was due to a missing namespace. After a little investigation, your suggestion works but raise another error: module_name_wrap.cpp:1858: warning : control reaches end of non-void function In the module_name_wrap.cpp file, SWIG generates something like: SWIGINTERN MyClass *new_MyClass(){ cout << "Hello World" << endl; } This can be solved by returning an instance of the C++ object at the end of the function: here is the final code: %extend MyClass { MyClass(){ cout << "Hello World" << endl; return new MyClass(); } }; %ignore MyClass::MyClass(); %include <MyClass.hpp> I hope this can help someone else. Thanks a lot, Fabien Rohrer -- View this message in context: http://www.nabble.com/Java---adding-code-to-a-constructor-tp20210601p20222605.html Sent from the swig-user mailing list archive at Nabble.com. |