This example tests SWIG's handling of C++ references. A reference in C++ is much like a pointer. Go represents C++ classes as pointers which are stored in interface values. Therefore, a reference to a class in C++ simply becomes an object of the class type in Go. For types which are not classes, a reference in C++ is represented as a pointer in Go.
In these cases, SWIG transforms everything into a pointer and creates a wrapper that looks like this in C++.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; }
or like this in Go:Vector wrap_addv(Vector *a, Vector *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:func Addv(arg1 Vector, arg2 Vector) Vector
or a method:Vector &operator[](int index);
For functions returning references, a wrapper like this is created:Vector &get(int index);
The following header file contains some class definitions with some operators and use of references.Vector *wrap_Object_get(Object *self, int index) { Vector &result = self->get(index); return &result; }
Click here to see a SWIG interface file with these additions.class VectorArray { public: ... %addmethods { Vector &get(int index) { return (*self)[index]; } void set(int index, Vector &a) { (*self)[index] = a; } } ... }