Establish a mapping between a managed object and a native pointer in c++/cli?
I have a c++/cli class in which I would like to maintain a mapping between a managed string and a native pointer.
Using std::map gives the compiler Warning C4368 (cannot define 'member' as 开发者_开发技巧a member of managed 'type': mixed types are not supported).
Using Dictionary gives C3225: generic type argument for 'TValue' cannot be 'native pointer', it must be a value type or a handle to a reference type
How can I achieve this mapping?
Just make a value type which holds the native pointer, i.e.
value struct TValue { native* ptr; };
Dictionary<String^, TValue> d;
Dictionary<String^, IntPtr>
is your best bet. Unfortunately, IntPtr
is conceptually equivalent to void*
, so you lose type information and will have to cast the value to the real pointer type every time you want to use it.
精彩评论