开发者

SWIG support for inheritance of static member functions

SWIG doesn't wrap inherited static functions of derived classes. How it can be resolved?

Here is a simple illustration of the problem.

This is a simple C++ header file:

// file test.hpp
#include <iostream>

class B
{
public:
  static void stat()
  { std::cerr << "=== calling static function B::stat" << std::endl; }

  void nonstat() const
  { std::cerr << "==== calling B::nonstat for some object of B" << std::endl; }
};

class D : public B {};

The C++ source file just includes the header file:

// file test.cpp
#include "test.hpp"

The SWIG interface file just includes the C++ header file:

// file test.swig
%module test
%{
#include "test.hpp"
%}

%include "test.hpp"

Then I generate the swig wrapper code by this:

swig -c++ -tcl8 -namespace main.swig

And then I create a shared library by this:

g++ -fpic -Wall -pedantic -fno-strict-aliasing \
               test.cpp test_wrap.cxx -o libtest.so

So when loading libtest.so in a tcl interpretor and trying to use the wrapped interface, it has the following behavior:

% load libtest.so test开发者_StackOverflow中文版
% test::B b
% test::D d
% b nonstat    # works fine
% d nonstat    # works fine
% test::B_stat # works fine
% test::D_stat # DOESN'T WORK !!

So the question is how can i make SWIG to wrap D::stat?


The static function is only defined in the parent class B correct? as in:

D::stat();

Is not callable correct? Thats why SWIG doesn't wrap the function...

As to how you can get access to the function, SWIG allows you to add/hide/wrap functions from any class you want to, so it would be possible to "fix" the SWIG class to give access to stat().

Believe the syntax is something like:

%extend D {
   ...
}

Its been a while since I touched SWIG so I might be misremembering something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜