4

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 Answers2

6

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

value struct TValue { native* ptr; };

Dictionary<String^, TValue> d;
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

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.

ildjarn
  • 62,044
  • 9
  • 127
  • 211