Gcroot Collection - item access

I make a bridge from .NET to C ++ and use Collection as a public element like this:

gcroot<System::Collections::ObjectModel::Collection<BModel::Device ^> ^> Devices; 

I use gcroot because my C ++ class is not managed (this is MFC), but I am having problems accessing it. When i do:

 Devices[x]->devicename 

I have an error:

Error 6 of error C2676: binary '[': 'gcroot' does not define this operator or conversion to a type acceptable for a predefined operator

So, I think I should somehow access the element in the collection, but not with these brackets: []

So how to access the gcroot clr collection item?

+4
source share
1 answer

While the member access operator, -> overloaded, it looks as if the subscript operator [] not, first expand gcroot .

 using namespace System::Collections::ObjectModel; Collection<BModel::Device ^> ^d = Devices; d[0] //... this should work 

The above is an implicit listing (so you cannot use auto ). You can use static_cast if you need a single liner:

 static_cast<Collection<BModel::Device ^> ^>(Devices)[0]; 
+7
source

All Articles