![]()
|
SWIG/Examples/python/reference/
C++ Reference Handling$Header: /cvs/projects/SWIG/Examples/python/reference/index.html,v 1.1 2000/09/01 20:39:29 beazley Exp $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. Some examplesReferences are most commonly used as function parameter. For example, you might have an operator like this:
or a function:
In these cases, SWIG transforms everything into a pointer and creates a wrapper
that looks like this:
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);
The following header file contains some class
definitions with some operators and use of references.
SWIG InterfaceSWIG does NOT support overloaded operators so it can not directly build an interface to the classes in the above file. However, a number of workarounds can be made. For example, an overloaded operator can be stuck behind a function call such as the addv() function above. Array access can be handled with a pair of set/get functions like this:
Click here to see a SWIG interface file with these additions.
Sample Python scriptClick here to see a script that manipulates some C++ references.Notes:
|