3
<title>SWIG:Examples:perl5:reference</title>
6
<body bgcolor="#ffffff">
9
<tt>SWIG/Examples/perl5/reference/</tt>
12
<H2>C++ Reference Handling</H2>
14
<tt>$Header: /cvs/projects/SWIG/Examples/perl5/reference/index.html,v 1.1 2000/09/01 20:48:27 beazley Exp $</tt><br>
17
This example tests SWIG's handling of C++ references. Since C++
18
references are closely related to pointers (as both refer to a
19
location in memory), SWIG simply collapses all references into
20
pointers when creating wrappers.
22
<h2>Some examples</h2>
24
References are most commonly used as function parameter. For example,
25
you might have an operator like this:
29
Vector operator+(const Vector &a, const Vector &b) {
43
Vector addv(const Vector &a, const Vector &b) {
53
In these cases, SWIG transforms everything into a pointer and creates a wrapper
58
Vector wrap_addv(Vector *a, Vector *b) {
64
Occasionally, a reference is used as a return value of a function
65
when the return result is to be used as an lvalue in an expression.
66
The prototypical example is an operator like this:
70
Vector &operator[](int index);
78
Vector &get(int index);
82
For functions returning references, a wrapper like this is created:
86
Vector *wrap_Object_get(Object *self, int index) {
87
Vector &result = self->get(index);
93
The following <a href="example.h">header file</a> contains some class
94
definitions with some operators and use of references.
96
<h2>SWIG Interface</h2>
98
SWIG does NOT support overloaded operators so it can not directly build
99
an interface to the classes in the above file. However, a number of workarounds
100
can be made. For example, an overloaded operator can be stuck behind a function
101
call such as the <tt>addv()</tt> function above. Array access can be handled
102
with a pair of set/get functions like this:
110
Vector &get(int index) {
111
return (*self)[index];
113
void set(int index, Vector &a) {
122
Click <a href="example.i">here</a> to see a SWIG interface file with these additions.
124
<h2>Sample Perl script</h2>
126
Click <a href="example.pl">here</a> to see a script that manipulates some C++ references.
131
<li>C++ references primarily provide notational convenience for C++
132
source code. However, it doesn't much matter to Perl.
135
<li>When a program returns a reference, a pointer is returned.
136
Unlike return by value, memory is not allocated to hold the
140
<li>SWIG has particular trouble handling various combinations of references
141
and pointers. This is side effect of an old parsing scheme and
142
type representation that will be replaced in future versions.