2015-08-25 26 views
1

鑑於C++函數痛飲通行證的std ::由參考矢量/出從C#到C++

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

而痛飲接口文件映射的std ::矢量在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 ::矢量從C++作爲ref或out PARAM映射到C#?

回答

1

你感到羞恥的計算器沒有答案!

無論如何,答案是,如果其他人有興趣......如果你在C#中創建VectorUInt32類型並將其傳遞給C++函數,它將通過引用傳遞,因此可以在不帶ref或out的情況下在C++中進行修改PARAM。

接口變爲:

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! 
+1

作品對我來說太。小的澄清:它的工作原理是因爲SWIG生成C#VectorUInt32作爲引用類型(因爲類不是結構體),所以C#Foo不需要爲_result使用ref(或out)修飾符。 – Sonic78