I Have a question regarding swig wrapped objects generated on the Python side and wrapped objects generated on the C++ side. Suppose I have the following simple C++ class definitions
#include <vector>
class Sphere
{
public:
Sphere(){};
};
class Container
{
public:
Container() : data_(0) {};
void add() {
data_.push_back(Sphere());
}
Sphere & get(int i) { return data_[i]; }
std::vector<Sphere> data_;
};
and the following swig setup
%module engine
%{
#define SWIG_FILE_WITH_INIT
#include "sphere.h"
%}
// -------------------------------------------------------------------------
// Header files that should be parsed by SWIG
// -------------------------------------------------------------------------
%feature("pythonprepend") Sphere::Sphere() %{
print 'Hello'
%}
%include "sphere.h"
If I then do the following in Python
import engine
sphere_0 = engine.Sphere()
container = engine.Container()
container.add()
sphere_1 = container.get(0)
Then the first instantiation of the wrapped Sphere class does call the init method of the Python wrapping interface ('Hello' is printed).
However, the second, where the instance is generated on the C++ side does not ('Hello' is not printed).
Since my goal is to be able to add additional Python functionality to the object upon its construction, I'd be pleased to hear if anybody has any pointers for a correct approach to achieve this - for both of the above instantiation scenarios.
Best regards,
Mads
sphere_1(the last line of your example)?