4
votes

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?

2

2 Answers

6
votes

Just make a value type which holds the native pointer, i.e.

value struct TValue { native* ptr; };

Dictionary<String^, TValue> d;
3
votes

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.