Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcroot Collection - element access

I'm making bridge from .NET to C++ and using Collection as a public element like this :

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

I'm using gcroot because my c++ class is not managed (it's MFC) but I'm having trouble with accessing it. When I am doing :

Devices[x]->devicename

I've got error :

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

So I guess I must access collection element somehow but not with this brackets : [ ]

So how to access gcroot clr collection element ?

like image 654
cnd Avatar asked Feb 23 '23 22:02

cnd


1 Answers

While the member access operator, -> is overloaded, it appears as if the subscript operator [] isn't, unwrap your gcroot first.

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

The above represents an implicit cast (this is why you cannot use auto). You can use static_cast if you need one-liner:

static_cast<Collection<BModel::Device ^> ^>(Devices)[0];
like image 138
John Leidegren Avatar answered Mar 02 '23 00:03

John Leidegren