Search code examples
javac++inheritanceswigprivate-members

Cannot expose private base class member functions in SWIG wrapper


I'm wrapping some C++ code with SWIG for an Android application. I'm facing an issue when I use a class that privately inherit from another, and throw a couple of using directive in there to expose some of the parent's member functions. Looks like this:

#include "Bar.h"

class Foo : private Bar {

//Stuff Foo-specific...

public:
    using Bar::baz;
};

The thing is, when I run SWIG, I get the following message during the wrapping:

Foo.h:8: Warning 315: Nothing known about 'Bar::baz()'.

Note: Both headers are included in the wrapper file, only the Foo header is wrapped, as I don't want the Bar header to be wrapped, my .i file looks like:

%{
    #include "Bar.h"
    #include "Foo.h"
%}

%include "Foo.h"

Then, my Java class doesn't compile because it cannot find this symbol...

I read in the SWIG documentation that private inheritance and the using keyword (although there isn't an example about private inheritance) are supposed to be supported, so what am I missing here?


Solution

  • First, your using statement should be using Bar::baz;.

    Anyway, as SWIG says in the warning, it cannot wrap Foo::baz() if it does not know the declaration in Bar::baz().

    Therefore, you need to show the declaration to SWIG, e.g., by an %include "Bar.h" directive. If you do not want Bar to be wrapped, you can use an additional %ignore Bar; directive.

    Here is a minimal working example:

    %ignore Bar;
    %inline %{
    class Bar {
    public:
      double baz() { return 4.2; }
    };
    class Foo : private Bar {
    public:
      using Bar::baz;
    };
    %}