繁体   English   中英

SWIG支持继承静态成员函数

[英]SWIG support for inheritance of static member functions

SWIG不会包装派生类的继承静态函数。 怎么解决?

以下是该问题的简单说明。

这是一个简单的C ++头文件:

// 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 {};

C ++源文件只包含头文件:

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

SWIG接口文件只包含C ++头文件:

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

%include "test.hpp"

然后我通过这个生成swig包装器代码:

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

然后我通过这个创建一个共享库:

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

因此,当在tcl解释器中加载libtest.so并尝试使用包装的接口时,它具有以下行为:

% load libtest.so test
% 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 !!

所以问题是如何让SWIG包装D :: stat?

静态函数只在父class B定义正确吗? 如:

D::stat();

不可校正吗? 这就是为什么SWIG没有包装功能......

至于如何访问该函数,SWIG允许您从任何类中添加/隐藏/包装函数,因此可以“修复”SWIG类以访问stat()

相信语法是这样的:

%extend D {
   ...
}

自从我触摸SWIG以来已经有一段时间了,所以我可能会错误地记住一些东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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