简体   繁体   中英

inheritance with virtual protected method

I am trying to wrapper some classes from c++ to c# with swig and it is work pretty good.
Suddenly I tried to call some method and my program crash.
My class flow looks like this(only the important classes): c++ code:

class B
{
public:
    virtual void f1();
protected:
    int f2();
};
class D:public B
{
public:
    virtual void f1();
protected:
    float f3();
};

This classes make swig to generate for each class a SwigDirector.
And in my c# code I try to do something like this:
public class Foo:D { void callF2Meth() { this.f2(); } }

The exception is thrown on d.f2(); In the code that swig generate the method to call f2 look like this(with out initialization): arg1 = (Base *)jarg1; darg = dynamic_cast<SwigDirector_B *>(arg1); result = (int)(darg)->f2(); arg1 = (Base *)jarg1; darg = dynamic_cast<SwigDirector_B *>(arg1); result = (int)(darg)->f2();
The exception is thrown because arg1 is type of SwigDirector_D that not inheritance from SwigDirector_B but from D(witch also do not inheritance from SwigDirector_B but from B)
The swig code:

  %module(directors="1", allprotected="1") SwigExample
  %{
  #include "Base.h"
  #include "Derived.h"
  %}
  %feature("director") Base;
  %feature("director") Derived;
  %include "Base.h"
  %include "Derived.h"

So I did some debugging and I realized that swig trying to convert SwigDirector of D to SwigDirector of B witch not in the same hierarchy.
How can I tell swig to correct wrapping the classes?

This seems to be a known limitation/bug in SWIG (see my question on the SWIG-mailinglist ). I chose to workaround this issue by implementing D::f2(){ return B::f2(); }.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM