This example tests SWIG's handling of C++ references. Since C++ references are closely related to pointers (as both refer to a location in memory), SWIG simply collapses all references into pointers when creating wrappers.
Vector operator+(const Vector &a, const Vector &b) {
Vector result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
or a function:
Vector addv(const Vector &a, const Vector &b) {
Vector result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
In these cases, SWIG transforms everything into a pointer and creates a wrapper
that looks like this:
Vector wrap_addv(Vector *a, Vector *b) {
return addv(*a,*b);
}
Occasionally, a reference is used as a return value of a function
when the return result is to be used as an lvalue in an expression.
The prototypical example is an operator like this:
or a method:Vector &operator[](int index);
For functions returning references, a wrapper like this is created:Vector &get(int index);
Vector *wrap_Object_get(Object *self, int index) {
Vector &result = self->get(index);
return &result;
}
The following header file contains some class
definitions with some operators and use of references.
class VectorArray {
public:
...
%extend {
Vector &get(int index) {
return (*self)[index];
}
void set(int index, Vector &a) {
(*self)[index] = a;
}
}
...
}
Click here to see a SWIG interface file with these additions.