c# - 通过 ref/out 从 C# 到 C++ 的 Swig 传递 Std::vector

标签 c# c++ swig

给定C++函数

void Foo(unsigned int _x, unsigned int _y, std::vector< unsigned int > &_results)

以及将 std::vector 映射到 C# 中类型 VectorUInt32 的 Swig 接口(interface)文件

%include "std_vector.i"

namespace std {
   %template(VectorUInt32) vector<unsigned int>;
};

我在 C# 代码中得到以下结果:

public static void Foo(uint _x, uint _y, VectorUInt32 _results)

这很棒,但我真正希望的是:

public static void Foo(uint _x, uint _y, out VectorUInt32 _results)

有谁知道如何将 std::vector 从 C++ 映射到 C# 作为 ref 或 out 参数?

最佳答案

Stackoverflow 没有答案,真可耻!

无论如何,答案是,如果其他人有兴趣...如果您在 C# 中创建 VectorUInt32 类型并将其传递给 C++ 函数,它是通过引用传递的,因此可以在 C++ 中修改而无需 ref 或 out 参数.

界面变成:

C++

void Foo(unsigned int _x, unsigned int _y, std::vector< unsigned int > &_results)

接口(interface)文件

%include "std_vector.i"

namespace std {
   %template(VectorUInt32) vector<unsigned int>;
};

C#

public static void Foo(uint _x, uint _y, VectorUInt32 _results)

用法

// Create and pass vector to C++
var vec = new VectorUInt32();
Foo(x, y, vec);
// do something with vec, its been operated on!

关于c# - 通过 ref/out 从 C# 到 C++ 的 Swig 传递 Std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32206339/

相关文章:

c++ - 从基调用派生类的隐藏(非虚拟)方法

python - 带有指针结构的 SWIG 函数

c++ - SWIG:如何将 boost::shared_ptr 包装到 std::vector?

c# - datagridview 单元格点击事件

c# - 自己的服务 - 没有注册类型的服务

c++ - 如何查找 boost 图中是否存在顶点?

c++ - Erlang - 向 UDP 多播 session 发送消息

python - SWIG 链接器: undefined symbol :_ZN2cv8fastFreeEPv (cv::fastFree(void*))

c# - 使用存储过程最优雅的方法是什么?

c# - 可重用的 Linq to XML 方法来过滤我的查询结果