From 26cc1ea36ef549b4262b18f4f66ec4d1ccbaa9e6 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 21 Mar 2014 17:56:15 +0100 Subject: [PATCH] scilab: document C++ inheritance --- Doc/Manual/Scilab.html | 81 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index 165f44667..9ff29b4ba 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -30,8 +30,8 @@
  • Constants and enumerations
  • Pointers
  • Structures -
  • Arrays
  • C++ classes +
  • C++ inheritance
  • C++ templates
  • C++ STL @@ -724,7 +724,7 @@ public:

    -can be used from Scilab like this: +can be used in Scilab like this:

    @@ -738,15 +738,88 @@ ans  =
     --> delete_Point(p2);
     
    +

    37.3.9 C++ inheritance

    -

    37.3.9 C++ Templates

    +

    +Inheritance is supported. SWIG knows the inheritance relationship between classes. +

    + +

    +A function is only generated for the class in which it is actually declared. +But if one of its parameter is a class, any instance of a derived class is accepted as argument. +

    + +

    +This mechanism applies also for accessor functions : these one are generated only in the class in which they are defined. +But any instance of a child class can be used as argument of these accessor functions. +

    + +

    +For example, let's take a base class Shape and two child classes Circle and Square: +

    + +
    +%module example
    +
    +%inline %{
    +
    +class Shape {
    +public:
    +  double x, y;
    +	void set_location(double _x, double _y) { x = _x; y = _y; }
    +  virtual double get_perimeter() { return 0; };
    +};
    +
    +class Circle : public Shape {
    +public:
    +	int radius;
    +  Circle(int _radius): radius(_radius) {};
    +  virtual double get_perimeter() { return 6.28 * radius; }
    +};
    +
    +class Square : public Shape {
    +public:
    +	int size;
    +  Square(int _size): size(_size) {};
    +  virtual double get_perimeter() { return 4 * size; }
    +};
    +
    +%}
    +
    + +

    +To set the location of the Circle, we have to use the function set_location() of the parent Shape. +But we can use either use the get_perimeter() function of the parent class or the child class: +

    + +
    +--> c = new_Circle(3);
    +
    +--> Shape_set_location(c, 2, 3);
    +--> Shape_x_get(c)
    + ans  =
    +
    +    2.
    +
    +--> Circle_get_perimeter(c)
    + ans =
    +
    +    18.84
    +
    +--> Shape_get_perimeter(c)
    + ans =
    +
    +    18.84
    +
    + +

    37.3.10 C++ Templates

    Templates are supported. See the SWIG general documentation on how templates are interfaced in SWIG.
    An example of templates can be found in Examples/scilab/templates.

    -

    37.3.10 C++ STL

    +

    37.3.11 C++ STL

    The Standard Template Library (STL) is partially supported. See STL for more details.