SWIG:Examples:tcl:class

`SWIG/Examples/tcl/class/`

## Wrapping a simple C++ class

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++ -tcl example.i

## Some sample Tcl scripts

SWIG performs two forms of C++ wrapping-- a low level interface and a high level widget-like interface.

- Click here to see a script that calls the C++ functions using the low-level interface.
- Click here to see the same script written with the high-level interface.

## Key points

- The low-level C++ interface works like this:

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

    >     set c [new_Circle 10.0]

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

    >     Shape_x_set $c 15        ;# Set member data
    >     set x [Shape_x_get $c]   ;# Get member data

    Note: when accessing member data, the name of the base class must be used such as `Shape_x_get`

  - To invoke a member function, you simply do this

    >     puts "The area is [Shape_area $c]"

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

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

  - To invoke a destructor, simply do this

    >     delete_Shape $c     # Deletes a shape

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

    >     set n $Shape_nshapes    # Get a static data member
    >     set Shapes_nshapes 13   # Set a static data member

- The high-level interface works like a Tk widget

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

    >     Circle c 10      # c becomes a name for the Circle object

  - To access member data, use cget and configure methods. For example:

    >     c configure -x 15        ;# Set member data
    >     set x [c cget -x]        ;# Get member data

  - To invoke a member function, you simply do this

    >     puts "The area is [c area]"

  - To invoke a destructor, simply destroy the object name like this:

    >     rename c ""         # c goes away

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

    >     set n $Shape_nshapes    # Get a static data member
    >     set Shapes_nshapes 13   # Set a static data member

## General Comments

- The low-level function interface is much faster than the high-level interface. In fact, all the higher level interface does is call functions in the low-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.
- C++ Namespaces - %nspace isn't yet supported for Tcl.