`SWIG/Examples/python/class/`

## Wrapping a simple C++ class

`/cvsroot/SWIG/Examples/python/class/index.html,v 1.3 2002/11/30 22:10:04 beazley Exp`

This example illustrates the most primitive form of C++ class wrapping performed by SWIG. In this case, C++ classes are simply transformed into a collection of C-style functions that provide access to class members.

## The C++ Code

Suppose you have some C++ classes described by the following (and admittedly lame) header file:

>     /* File : example.h */

>     class Shape {
>     public:
>       Shape() {
>         nshapes++;
>       }
>       virtual ~Shape() {
>         nshapes--;
>       };
>       double  x, y;
>       void    move(double dx, double dy);
>       virtual double area() = 0;
>       virtual double perimeter() = 0;
>       static  int nshapes;
>     };

>     class Circle : public Shape {
>     private:
>       double radius;
>     public:
>       Circle(double r) : radius(r) { };
>       virtual double area();
>       virtual double perimeter();
>     };

>     class Square : public Shape {
>     private:
>       double width;
>     public:
>       Square(double w) : width(w) { };
>       virtual double area();
>       virtual double perimeter();
>     };

## The SWIG interface

A simple SWIG interface for this can be built by simply grabbing the header file like this:

>     /* File : example.i */
>     %module example

>     %{
>     #include "example.h"
>     %}

>     /* Let's just grab the original header file here */
>     %include "example.h"

Note: when creating a C++ extension, you must run SWIG with the `-c++` option like this:

>     % swig -c++ -python example.i

## A sample Python script

Click here to see a script that calls the C++ functions from Python.

## Key points

- To create a new object, you call a constructor like this:

  >     c = example.new_Circle(10.0)

- To access member data, a pair of accessor functions are used. For example:

  >     example.Shape_x_set(c,15)    # Set member data
  >     x = example.Shape_x_get(c)    # Get member data

  Note: when accessing member data, the name of the class in which the member data was must be used. In this case, `Shape_x_get()` and `Shape_x_set()` are used since 'x' was defined in Shape.

- To invoke a member function, you simply do this

  >     print "The area is ", example.Shape_area(c)

- Type checking knows about the inheritance structure of C++. For example:

  >     example.Shape_area(c)       # Works (c is a Shape)
  >     example.Circle_area(c)      # Works (c is a Circle)
  >     example.Square_area(c)      # Fails (c is definitely not a Square)

- To invoke a destructor, simply do this

  >     example.delete_Shape(c)     # Deletes a shape

  (Note: destructors are currently not inherited. This might change later).

- Static member variables are wrapped as C global variables. For example:

  >     n = example.cvar.Shape_nshapes     # Get a static data member
  >     example.cvar.Shapes_nshapes = 13   # Set a static data member

## General Comments

- This low-level interface is not the only way to handle C++ code. Shadow classes provide a much higher-level interface.

- SWIG *does* know how to properly perform upcasting of objects in an inheritance hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass an object of a derived class to any function involving a base class.

- A wide variety of C++ features are not currently supported by SWIG. Here is the short and incomplete list:

  - Overloaded methods and functions. SWIG wrappers don't know how to resolve name conflicts so you must give an alternative name to any overloaded method name using the %name directive like this:

    >     void foo(int a);
    >     %name(foo2) void foo(double a, double b);

  - Overloaded operators. Not supported at all. The only workaround for this is to write a helper function. For example:

    >     %inline %{
    >         Vector *vector_add(Vector *a, Vector *b) {
    >               ... whatever ...
    >         }
    >     %}

  - Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).

  - Dave's snide remark: Like a large bottle of strong Tequilla, it's better to use C++ in moderation.