C++ to Python and returning members of a C++ object

Terry Barnaby <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
I have a class library that has List objects as members of the class. If 
I use this class in a Python function and try to return one of these 
more complex members all hell breaks loose. It appears the SWIG 
generated wrapper returns a pointer to the objects member rather than 
making a copy of it when returning the member from the function.

So I have something like:

class TypeB {

public:

     List<TypeA> list1;

};

If in Python I have a function like the following, the returned item is 
effectively (SWIG wrapped) a pointer the b objects member and b 
disappears when the function returns.

def func1():

     b = TypeB();

     b.list1.append(TypeA());

     return b.list1;

Now I can use "return ListTypeA(b.list1)", where ListTypeA has been 
declared using a %template declaration such that the copy constructor is 
called to explicitly make a copy of the list, but is there a better way 
of handing this ?

I know this is difficult to handle, but perhaps something like a 
%template(return) to add code when returning a type in a Python function 
somehow or maybe the overall objects reference counter can be incremented ?



_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.