[Mono-list] How to expose C++ virtual functions?

J. Perkins jason@379.com
17 Jan 2003 10:43:13 -0500


Hello all,

I am tinkering around with my wxWindows wrapper, and I've gotten myself
a bit stumped. A lot of the wxw classes rely on inheritance and virtual
functions to delegate implementation details. For instance, wxApp has a
virtual called OnInit() that is called to setup the application.
Catching the virtual call and passing it up to C# isn't terribly
difficult: I just store a delegate and call it. But what happens if C#
needs to call the base class implementation? Does that make sense? Maybe
I should include some code just in case.

If I wrap wxApp something like this:

   class _App : public wxApp
   {
      virtual bool OnInit()
      {
         // Call the OnInit delegate (assume I set it previously)
         return OnInitDelegate();
      }
   }

And in C#, the delegate calls a function like this:

   class App
   {
      IntPtr wxAppPtr;   // A C pointer to the underlying wxApp instance

      public virtual bool OnInit()
      {
         if (!base.OnInit())
            return false;

         // Set up my application
      }
   }

How do I get "base.OnInit()" to call wxApp::OnInit()? The obvious, but
wrong, solution would be to have base:OnInit() call an unmanaged C
function like this:

   bool _App_OnInit(wxApp* theApp) 
   {
      return theApp->OnInit();
   }

But that will just end up in the _App::OnInit() virtual function again.
I think this is the last issue I need to resolve; I am hoping that
someone else has solved it before me.

Thanks!

Jason
379