我正在搭建从 .NET 到 C++ 的桥梁,并将 Collection 用作公共(public)元素,如下所示:
gcroot<System::Collections::ObjectModel::Collection<BModel::Device ^> ^> Devices;
我正在使用 gcroot,因为我的 C++ 类不是托管的(它是 MFC),但我在访问它时遇到了问题。当我在做的时候:
Devices[x]->devicename
我有错误:
Error 6 error C2676: binary '[' : 'gcroot' does not define this operator or a conversion to a type acceptable to the predefined operator
所以我想我必须以某种方式访问集合元素,但不能使用这个括号:[ ]
那么如何访问gcroot clr集合元素呢?
最佳答案
虽然成员访问运算符 ->
被重载,但下标运算符 []
似乎没有被重载,解开你的 gcroot
首先。
using namespace System::Collections::ObjectModel;
Collection<BModel::Device ^> ^d = Devices;
d[0] //... this should work
以上表示隐式转换(这就是为什么不能使用 auto
)。如果需要单行代码,可以使用 static_cast
:
static_cast<Collection<BModel::Device ^> ^>(Devices)[0];
关于.net - gcroot 集合 - 元素访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470612/