我正在开发一些使用托管C ++包装器和C#中的用户界面以本机C ++编写的软件。我需要将一些信息从本机代码传递到接口。我认为传递信息的最佳方式是在元组列表中。我知道在本机C ++中我需要使用list<tuple<..>>
,并且工作正常。我现在想要做的是,在包装器中获取该输出,并返回List<Tuple<..>>
System::Collections::Generic::List
和System::Tuple
,而不是来自本机的stl。我知道列表在语法上是非常不同的,但这应该不是问题。在C#代码中,编译器接受List<Tuple<..>>
,但在托管C ++代码中则不然。我这样做错了/我使用了错误的数据类型吗?任何帮助都会很棒!
答案 0 :(得分:3)
尝试这样的事情:
using namespace System::Collections::Generic;
// A List<Tuple<int, float>> in managed C++
public ref class TupleTest
{
public:
static List<Tuple<int, float>^>^ GetTuple() {
List<Tuple<int, float>^>^ ret = gcnew List<Tuple<int,float>^>();
Tuple<int,float>^ t = gcnew Tuple<int,float>(5, 2.6);
ret->Add(t);
return ret;
}
};
注意:如果您愿意,可以使用Int32和Single(或Double)代替int / float。
编辑:请注意^
运营商。这些表示C ++ / CLI引用类型。你会经常使用它们! (对于常规C ++中的指针,它有点像*
,但是意味着GC的引用类型)