gpt4 book ai didi

python - SWIG 忽略类中的 %mutable;如何解决?

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:50 24 4
gpt4 key购买 nike

在 Windows 上使用 SWIG 3.0.0,导出到 python,我有以下接口(interface)文件:

%module example

%immutable;

class MyClass {
public:
%mutable;
char *str1;
%immutable;
char *str2;
};

我的意图是使整个文件中的所有类成员默认不可变(只读),但允许一些可写——在本例中为 str1。

但是 SWIG 似乎忽略了类中的 %mutable 指令;它从不为 str1 或 str2 生成任何 MyClass_str1_set 方法。我已经阅读了文档,看起来这应该有效。我也试过将 %mutable MyClass::str1; 放在顶层,但这也无济于事。我错过了什么?

最佳答案

文档中不是很清楚,现在无法测试,但这可能有效:

// make everything immutable except str2 data member:
%feature("immutable","1");
%feature("immutable","0") MyClass::str2;

class MyClass {
public:
char *str1;
char *str2;
char *str3;
};

如果这不起作用,您还可以尝试将语句移到类中:

class MyClass {
public:

// make everything immutable except str2 data member:
%immutable;
char *str0;
char *str1;

%mutable;
char *str2;

%immutable;
char *str3;
char *str4;
};

关于python - SWIG 忽略类中的 %mutable;如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23298347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com