3
<title>SWIG:Examples:python:class</title>
6
<body bgcolor="#ffffff">
9
<tt>SWIG/Examples/python/class/</tt>
12
<H2>Wrapping a simple C++ class</H2>
14
<tt>$Header: /cvs/projects/SWIG/Examples/python/class/index.html,v 1.1.4.1 2001/08/30 04:12:40 beazley Exp $</tt><br>
17
This example illustrates the most primitive form of C++ class wrapping performed
18
by SWIG. In this case, C++ classes are simply transformed into a collection of
19
C-style functions that provide access to class members.
23
Suppose you have some C++ classes described by the following (and admittedly lame)
28
/* File : example.h */
39
void move(double dx, double dy);
40
virtual double area() = 0;
41
virtual double perimeter() = 0;
45
class Circle : public Shape {
49
Circle(double r) : radius(r) { };
50
virtual double area();
51
virtual double perimeter();
54
class Square : public Shape {
58
Square(double w) : width(w) { };
59
virtual double area();
60
virtual double perimeter();
65
<h2>The SWIG interface</h2>
67
A simple SWIG interface for this can be built by simply grabbing the header file
72
/* File : example.i */
79
/* Let's just grab the original header file here */
84
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
87
% swig -c++ -python example.i
91
<h2>A sample Python script</h2>
93
Click <a href="example.py">here</a> to see a script that calls the C++ functions from Python.
98
<li>To create a new object, you call a constructor like this:
102
c = example.new_Circle(10.0)
107
<li>To access member data, a pair of accessor functions are used.
112
example.Shape_x_set(c,15) # Set member data
113
x = example.Shape_x_get(c) # Get member data
117
Note: when accessing member data, the name of the class in which
118
the member data was must be used. In this case, <tt>Shape_x_get()</tt>
119
and <tt>Shape_x_set()</tt> are used since 'x' was defined in Shape.
122
<li>To invoke a member function, you simply do this
126
print "The area is ", example.Shape_area(c)
131
<li>Type checking knows about the inheritance structure of C++. For example:
135
example.Shape_area(c) # Works (c is a Shape)
136
example.Circle_area(c) # Works (c is a Circle)
137
example.Square_area(c) # Fails (c is definitely not a Square)
142
<li>To invoke a destructor, simply do this
146
example.delete_Shape(c) # Deletes a shape
150
(Note: destructors are currently not inherited. This might change later).
153
<li>Static member variables are wrapped as C global variables. For example:
157
n = example.cvar.Shape_nshapes # Get a static data member
158
example.cvar.Shapes_nshapes = 13 # Set a static data member
164
<h2>General Comments</h2>
167
<li>This low-level interface is not the only way to handle C++ code.
168
Shadow classes provide a much higher-level interface.
171
<li>SWIG *does* know how to properly perform upcasting of objects in
172
an inheritance hierarchy (including multiple inheritance). Therefore
173
it is perfectly safe to pass an object of a derived class to any
174
function involving a base class.
177
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
178
short and incomplete list:
182
<li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
183
conflicts so you must give an alternative name to any overloaded method name using the
184
%name directive like this:
189
%name(foo2) void foo(double a, double b);
194
<li>Overloaded operators. Not supported at all. The only workaround for this is
195
to write a helper function. For example:
200
Vector *vector_add(Vector *a, Vector *b) {
208
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
211
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
212
use C++ in moderation.