Swig Pass Std :: vector by ref / out from C#to C ++

时间:2015-08-25 14:10:48

标签: c# c++ swig

给定C ++函数

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

Swig接口文件将std :: vector映射到C#

中的VectorUInt32类型
%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 param?

1 个答案:

答案 0 :(得分:1)

对你没有答案的Stackoverflow感到羞耻!

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

界面变为:

<强> C ++

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

接口文件

%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!