C++のクラスをC#で継承する。swig使用。

まさかこんなことができるとは思っていなかった。

やりたいこと

  • C++で定義されたコードをC#で継承してい使用する。

cpp

class Base {
public:
    virtual ~Base() {}
    virtual void tell() const = 0;
};

class Caller {
public:
   void apply(const Base& base) const {base.tell();}
};

*.i

%module (directors="1") SwigSample
%{
#include "../SwingSample.h"
%}

%feature("director") Base;

c#

    class MyBase: Base {
        public override void tell() 
        {
            Console.WriteLine("Yes!!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Caller c = new Caller ();
            MyBase b = new MyBase();
            c.apply(b);

        }
    }