c++ - SWIG 支持继承静态成员函数

标签 c++ inheritance tcl swig static-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 接口(interface)文件只包含 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 并尝试使用包装接口(interface)时,它具有以下行为:

% 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 类以访问 统计()

相信语法是这样的:

%extend D {
   ...
}

我已经有一段时间没有接触 SWIG 了,所以我可能记错了什么。

关于c++ - SWIG 支持继承静态成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3077236/

相关文章:

c++ - 如何将指针保存到重新定义的运算符?

c++ - boost 互斥锁的作用域解锁

c++ - 调用派生自抽象类的类的方法 [C++]

delphi - 检查对象是否继承自泛型类

c# - 将结构转换为继承自同一接口(interface)的类

c++ - 基于 C++ 特化的重载

python - tcl 和 Python 的相似之处

python - 当我向 python 服务器发送 "invalid command"时,TCL 客户端挂起

linux - 无效命令名称 "Agent/rtProto/OSPF"

c++ - 在简单的计算器中包含三角函数,C++